Esempio n. 1
0
 def heartbeatCheck(self):
     if self.isTimeOut():
         PLOG.debug("%s websocket timeout,disconnect it" % self.wsname)
         self.close()
         WSManager.RemoveWebSocket(self.wsname)
     else:
         PLOG.trace("send ping")
         self.ping("ping")
Esempio n. 2
0
 def heartbeatCheck(self):
     if self.isTimeOut():
         PLOG.debug("%s websocket timeout,disconnect it"%self.wsname) 
         self.close()
         WSManager.RemoveWebSocket(self.wsname)
     else:
         PLOG.trace("send ping")
         self.ping("ping")
Esempio n. 3
0
def querysql(sqltext,dboperater=None,how = 0):
    resultls = []
    try:
        if dboperater == None:
            dboperater = DBOperater()
        if dboperater.conn == None:
            dboperater.createconnection(host=sadb.host,user=sadb.dbuser,passwd=sadb.dbpwd,dbname=sadb.dbname) #数据库连接
        rowNum, result = dboperater.query(sqltext)
        PLOG.trace("%s query finish"%(sqltext))
        resultls = dboperater.fetch_queryresult(result,rowNum, how = how) 
    except MySQLdb.Error,e:
        PLOG.debug("Mysql Error %d: %s,sql=%s" %(e.args[0], e.args[1],sqltext)) 
        return None
Esempio n. 4
0
            items = []
            for p in self.programlist:
                items.append((p.name, p.status))
            fmt = '%-40s %9s'
            ret = '\n'.join([fmt % (x, '[%s]' % y) for x, y in items])
            PLOG.info("report runing status:\n%s" % ret)
        elif msg == "stop":
            pass
        return ret
class CF:
    pass
if __name__ == "__main__":
    conf = CF()
    conf.host = "127.0.0.1"
    conf.port = 24001
    PLOG.trace(assistant.SF("%s/log/ALS_$(Date8)_$(filenumber2).log" % (os.path.dirname(__file__))))
    PLOG.enableFilelog("%s/log/ALS_$(Date8)_$(filenumber2).log" % (os.path.dirname(__file__)))
    try:
        parentpid = os.getppid()
    except:
        print "no parent id!"
        sys.exit(1)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        s.bind((conf.host, conf.port))
        s.settimeout(1)
    except Exception, e:
        print("绑定UDP端口失败,Host=%s,Port=%d,err=%s" % (conf.host, conf.port, e))
        sys.exit(1)
    print ("Start listen UDP[%s:%d]..." % (conf.host, conf.port))
Esempio n. 5
0
 def on_pong(self,data):
     PLOG.trace("receive pong")
     self.updateActiveTime()
Esempio n. 6
0
 def on_message(self, message):
     PLOG.trace("receive msg %s"%message)
     self.updateActiveTime()
Esempio n. 7
0
 def on_pong(self, data):
     PLOG.trace("receive pong")
     self.updateActiveTime()
Esempio n. 8
0
 def on_message(self, message):
     PLOG.trace("receive msg %s" % message)
     self.updateActiveTime()
Esempio n. 9
0
            PLOG.info("report runing status:\n%s" % ret)
        elif msg == "stop":
            pass
        return ret


class CF:
    pass


if __name__ == "__main__":
    conf = CF()
    conf.host = "127.0.0.1"
    conf.port = 24001
    PLOG.trace(
        assistant.SF("%s/log/ALS_$(Date8)_$(filenumber2).log" %
                     (os.path.dirname(__file__))))
    PLOG.enableFilelog("%s/log/ALS_$(Date8)_$(filenumber2).log" %
                       (os.path.dirname(__file__)))
    try:
        parentpid = os.getppid()
    except:
        print "no parent id!"
        sys.exit(1)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        s.bind((conf.host, conf.port))
        s.settimeout(1)
    except Exception, e:
        print("绑定UDP端口失败,Host=%s,Port=%d,err=%s" % (conf.host, conf.port, e))
Esempio n. 10
0
        PLOG.debug("Mysql Error %d: %s,sql=%s" %(e.args[0], e.args[1],sqltext))  

def querysql(sqltext,dboperater=None,how = 0):
    resultls = []
    try:
        if dboperater == None:
            dboperater = DBOperater()
        if dboperater.conn == None:
            dboperater.createconnection(host=sadb.host,user=sadb.dbuser,passwd=sadb.dbpwd,dbname=sadb.dbname) #数据库连接
        rowNum, result = dboperater.query(sqltext)
        PLOG.trace("%s query finish"%(sqltext))
        resultls = dboperater.fetch_queryresult(result,rowNum, how = how) 
    except MySQLdb.Error,e:
        PLOG.debug("Mysql Error %d: %s,sql=%s" %(e.args[0], e.args[1],sqltext)) 
        return None
    PLOG.trace("%s query return"%(sqltext))
    return resultls

def executeproc(procsqlname,dboperater=None,args=None):
    resultls = []
    try:
        if dboperater == None:
            dboperater = DBOperater()
        if dboperater.conn == None:
            dboperater.createconnection(host=sadb.host,user=sadb.dbuser,passwd=sadb.dbpwd,dbname=sadb.dbname) #数据库连接
        cur = dboperater.conn.cursor()
        cur.callproc(procsqlname,args)
        dboperater.conn.commit() #提交SQL语句
        cur.close()
    except MySQLdb.Error,e:
        PLOG.debug("Mysql Error %d: %s,sql=%s" %(e.args[0], e.args[1],procsqlname)) 
Esempio n. 11
0
def statisticsCurrentDayData(daydate):
    nextday = daydate + datetime.timedelta(days=1)
    startquerytime = daydate
    endquerytime = daydate + datetime.timedelta(
        hours=SAPeakDataPublic.st.queryunit)
    while endquerytime <= nextday:
        acctquerysql = "select acctinputoctets,acctoutputoctets,acctstarttime,acctstoptime,regionid from %s where acctstarttime>='%s' and acctstarttime<'%s'"%\
                   (SAPeakDataPublic.sadb.tablename,startquerytime.strftime('%Y-%m-%d %H:%M:%S'),endquerytime.strftime('%Y-%m-%d %H:%M:%S'))
        PLOG.debug("sql=%s" % acctquerysql)
        startquerytime = endquerytime
        endquerytime = endquerytime + datetime.timedelta(
            hours=SAPeakDataPublic.st.queryunit)
        i = 0
        while i < SAPeakDataPublic.st.queryrepeattimes:
            res = SAPeakDataPublic.querysql(acctquerysql)
            if res != None:
                break
            else:
                i = i + 1
        if i == 3 or res == None:
            print(
                "%s statistics data failed! db query appear error %d consecutive times,please execute again later!"
                % (daydate.strftime('%Y-%m-%d'),
                   SAPeakDataPublic.st.queryrepeattimes))
            PLOG.info(
                "%s statistics data failed! db query appear error %d consecutive times,please execute again later!"
                % (daydate.strftime('%Y-%m-%d'),
                   SAPeakDataPublic.st.queryrepeattimes))
            return
        # 统计数据
        PLOG.trace("start statistics...")
        for row in res:
            if row[2] == None or row[3] == None or row[4] == None:
                PLOG.warn("lack essential data!skip this data")
                continue
            regionid = row[4]
            totalflow = 0
            if row[0] != None:
                totalflow += row[0]
            if row[1] != None:
                totalflow += row[1]
            if row[3].day > row[2].day:
                # 跨天
                endMinute = 23 * 60 + 59
            elif row[3].day < row[2].day:
                PLOG.info(
                    "stoptime day less than starttime day,invalid data,skip")
            else:
                endMinute = row[3].hour * 60 + row[3].minute
            startMinute = row[2].hour * 60 + row[2].minute
            #startMinute = datetime.datetime.strptime(row[2],'%Y-%m-%d %H:%M:%S')
            #endMinute = datetime.datetime.strptime(row[3],'%Y-%m-%d %H:%M:%S')
            totalMinute = endMinute - startMinute + 1
            if totalMinute <= 0:
                PLOG.info("stoptime less than starttime,invalid data,skip")
                continue
            if SAStopDefine.stopDc.stops.has_key(regionid):
                stop = SAStopDefine.stopDc.stops[regionid]
                startindex = startMinute
                endindex = endMinute
                flowOneMinute = float(totalflow) / totalMinute / 1024 / 1024
                index = startindex
                while index <= endindex:
                    stop.dayArray[index][0] += 1
                    stop.dayArray[index][1] += flowOneMinute
                    if stop.dayArray[index][0] > stop.peakonlinenum:
                        stop.peakonlinenum = stop.dayArray[index][0]
                        stop.peakonlinetime = datetime.datetime(
                            daydate.year, daydate.month, daydate.day,
                            index / 60, index % 60)
                    if stop.dayArray[index][0] > stop.peakbandwidth:
                        stop.peakbandwidth = stop.dayArray[index][1]
                        stop.peakbandwidthtime = datetime.datetime(
                            daydate.year, daydate.month, daydate.day,
                            index / 60, index % 60)
                    index += 1
        PLOG.trace("statistics end")
    # 数据处理结束,输出各站点峰值数据
    for stopid, stop in stopsCentor.stops.items():
        peakbandwidth = stop.peakbandwidth * 8 / 60
        print("%s %s %d %.2f" % (daydate.strftime('%Y-%m-%d'), stop.name,
                                 stop.peakonlinenum, peakbandwidth))
        PLOG.debug(
            "%s %s %d %.2f %s %s" %
            (daydate.strftime('%Y-%m-%d'), stop.name, stop.peakonlinenum,
             peakbandwidth, stop.peakonlinetime.strftime('%H:%M'),
             stop.peakbandwidthtime.strftime('%H:%M')))
Esempio n. 12
0
def statisticsCurrentDayData(daydate) :
    nextday = daydate+datetime.timedelta(days=1)
    startquerytime = daydate
    endquerytime = daydate+datetime.timedelta(hours=SAPeakDataPublic.st.queryunit)
    while endquerytime<=nextday:
        acctquerysql = "select acctinputoctets,acctoutputoctets,acctstarttime,acctstoptime,regionid from %s where acctstarttime>='%s' and acctstarttime<'%s'"%\
                   (SAPeakDataPublic.sadb.tablename,startquerytime.strftime('%Y-%m-%d %H:%M:%S'),endquerytime.strftime('%Y-%m-%d %H:%M:%S')) 
        PLOG.debug("sql=%s"%acctquerysql)
        startquerytime=endquerytime
        endquerytime=endquerytime+datetime.timedelta(hours=SAPeakDataPublic.st.queryunit)
        i = 0
        while i<SAPeakDataPublic.st.queryrepeattimes:            
            res = SAPeakDataPublic.querysql(acctquerysql)
            if res!=None:
                break
            else:
                i = i+1
        if i==3 or res==None:
            print("%s statistics data failed! db query appear error %d consecutive times,please execute again later!"%(daydate.strftime('%Y-%m-%d'),SAPeakDataPublic.st.queryrepeattimes))
            PLOG.info("%s statistics data failed! db query appear error %d consecutive times,please execute again later!"%(daydate.strftime('%Y-%m-%d'),SAPeakDataPublic.st.queryrepeattimes))
            return
        # 统计数据
        PLOG.trace("start statistics...")
        for row in res:
            if row[2] ==None or row[3] ==None or row[4] ==None:
                PLOG.warn("lack essential data!skip this data")
                continue          
            regionid = row[4]
            totalflow = 0
            if row[0]!=None:
                totalflow += row[0]
            if row[1]!=None:
                totalflow += row[1]
            if row[3].day > row[2].day:
                # 跨天
                endMinute = 23*60+59
            elif row[3].day < row[2].day:
                PLOG.info("stoptime day less than starttime day,invalid data,skip")
            else:
                endMinute = row[3].hour*60+row[3].minute 
            startMinute = row[2].hour*60+row[2].minute       
            #startMinute = datetime.datetime.strptime(row[2],'%Y-%m-%d %H:%M:%S')
            #endMinute = datetime.datetime.strptime(row[3],'%Y-%m-%d %H:%M:%S')
            totalMinute = endMinute-startMinute + 1
            if totalMinute <=0:
                PLOG.info("stoptime less than starttime,invalid data,skip")
                continue 
            if SAStopDefine.stopDc.stops.has_key(regionid):
                stop = SAStopDefine.stopDc.stops[regionid] 
                startindex = startMinute
                endindex = endMinute
                flowOneMinute = float(totalflow)/totalMinute/1024/1024
                index = startindex
                while index <= endindex:
                    stop.dayArray[index][0] += 1
                    stop.dayArray[index][1] += flowOneMinute
                    if stop.dayArray[index][0] > stop.peakonlinenum: 
                        stop.peakonlinenum = stop.dayArray[index][0]
                        stop.peakonlinetime = datetime.datetime(daydate.year,daydate.month,daydate.day,index/60,index%60)
                    if stop.dayArray[index][0] > stop.peakbandwidth:
                        stop.peakbandwidth = stop.dayArray[index][1]
                        stop.peakbandwidthtime = datetime.datetime(daydate.year,daydate.month,daydate.day,index/60,index%60)
                    index += 1
        PLOG.trace("statistics end")
    # 数据处理结束,输出各站点峰值数据
    for stopid,stop in stopsCentor.stops.items():
        peakbandwidth = stop.peakbandwidth*8/60
        print("%s %s %d %.2f"%(daydate.strftime('%Y-%m-%d'),stop.name,stop.peakonlinenum,peakbandwidth))
        PLOG.debug("%s %s %d %.2f %s %s"%(daydate.strftime('%Y-%m-%d'),stop.name,stop.peakonlinenum,peakbandwidth,stop.peakonlinetime.strftime('%H:%M'),stop.peakbandwidthtime.strftime('%H:%M')))