Urban48 0 Newbie Poster

hello!
I'm developing a software to monitor communication to different locations.
the principle is simple: send ping every second and display the results in real time (ms delay, packet loss etc)

It's also important to mention that I'm running the software from Linux, so in order to ping from my software i choose the subporocess.Popen way, because opening sockets require you to be logged as root user. And i don't want to give everyone root access to the server..

Here is the class reponsible for the pinging:

class WorkerThread(QThread):
  def __init__(self,receiver,sitename):
    QThread.__init__(self)

    global time_manager
    time_manager[sitename] = [time.time(),0,0] #for statistic purpeses

    self.stopped = 0
    self.receiver = receiver
    self.sitename = sitename


  def run(self):
    icmp_count = 0
    ping_result = ""
    packeloss_result = ""

    while not self.stopped:
       data = subprocess.Popen("ping -c1 "+str(sites[self.sitename]),shell = True,stdout=subprocess.PIPE)
       data.wait()
       time_manager[self.sitename][1] +=1 #counts the icmps sent 
       bufferdata = data.stdout.read() 
       ping_result = ms_pat.findall(bufferdata)
       packeloss_result = packetloss_pat.findall(bufferdata)
    
       if ping_result:
         ping_ms = ping_result[0][0]
       if packeloss_result:
         time_manager[self.sitename][2] +=1        
         ping_ms = "-1"
       
       ms_count[self.sitename].append(float(ping_ms))
       time.sleep(1)
       event = QCustomEvent(12345)
       event.setData(self.sitename+ping_ms)
       QApplication.postEvent(self.receiver, event)

  def stop(self):
    self.stopped = 1

I'm using threads because sometimes i need to run multiple ping jobs to different sites.

My problem is this:
when running i get the ms delay results perfectly, but every few pings i get a not accurate result, higher then what it actually should be.

Example:
ping_ms = 20.0
ping_ms = 21.31
ping_ms = 23.23
ping_ms = 24.2
ping_ms = 80.2
ping_ms = 20.0
ping_ms = 21.31
ping_ms = 23.23
ping_ms = 24.2

I don't understand why this is happening. if someone could help me, it would be much appreciated

thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.