def runMonitor(id,url,code,returnMsg): b = businessMonitor(url,code,returnMsg) data = b.runMonitor() curTime = str(int(time.time())) rrdPath = os.path.dirname(os.path.realpath(__file__))+"/rrd/businessMonitoring_"+id+".rrd" #update rrd namelookupTime = str(round(data["namelookupTime"],2)) startTransferTime = str(round(data["startTransferTime"],2)) totalTime = str(round(data["totalTime"],2)) connectTime = str(round(data["connectTime"],2)) if data["httpCode"]==200 or data["httpCode"]==302: try: rrdtool.updatev(rrdPath,"%s:%s:%s:%s:%s"%(curTime,namelookupTime,startTransferTime,totalTime,connectTime)) except Exception,e: print str(e)
def new_page(ipaddr): try: ipmi=ipmitool(ipaddr[0],ipaddr[1],ipaddr[2]) except: return 0 web_ip=ipmi.execute('sensor') print('working...') print("ip network ") msg=ipmi.output print(msg) mynote='/home/zhangshuo/sub/ipmitest/ipmitool/ipmi/view/'+str(ipaddr[0])+'.txt' mynote="".join(mynote.split()) putfile=open(mynote,'a') outputfile=open(mynote,'wb') line=outputfile.write(msg) #line = fo.write( str ) outputfile.flush() outputfile.close() del(outputfile) # os.system('bash new_graph.sh') print("正在进行数据采集....") mylist=[] def execCmd(cmd): r = os.popen(cmd) text = r.read() r.close() return text #myCmd="cat /home/zhangshuo/sub/ipmitest/ipmitool/ipmi/view/"+str(ipaddr[0])+".txt | awk '{print $1,$3,$4}'" myCmd="/home/zhangshuo/sub/ipmitest/ipmitool/ipmi/view/"+str(ipaddr[0]) myCmd="".join(myCmd.split()) myCmd="sudo cat "+myCmd+".txt | awk '{print $1,$3,$4}'" # mystr=execCmd("cat new.txt | awk '{print $1,$3,$4}'") mystr=execCmd(myCmd) mystr=mystr.split() num=len(mystr) print(num) i=2 new_number=[] while i<num : mylist.append(mystr[i]) if len(mystr[i]) > 3 : print(mystr[i]) new_num=float(mystr[i]) print(new_num) new_num=int(new_num) new_number.append(new_num) i=i+3 print(new_number) queue=list(new_number) starttime=int(time.time()) myrrd_text='/home/zhangshuo/sub/ipmitest/ipmitool/ipmi/rrdtext/'+str(ipaddr[0])+'.rrd' myrrd_text="".join(myrrd_text.split()) print(myrrd_text) update=rrdtool.updatev(myrrd_text,'%s:%s:%s:%s:%s:%s:%s' % (str(starttime),str(queue[0]),str(queue[1]),str(queue[2]),str(queue[3]),str(queue[4]),str(queue[5]))) print(update)
def nic_rrd(self,sql_nic): # mysql configure host_sql = self.host user = self.user password = self.password defaultdb = self.defaultdb # rrdtool host configure host = self.rrdtool_host report_dir = self.rrdtool_dir conn = MySQLdb.connect(host_sql,user,password,defaultdb) cur = conn.cursor() nic_list = [] cur.execute(sql_nic) rows = int(cur.rowcount) for i in xrange(rows): row = cur.fetchone() #print row[0],row[1],row[2] a = str(row[0]) e = str(row[1]) timeArray = time.strptime(e, "%Y-%m-%d %H:%M:%S") b = str(int(time.mktime(timeArray))) c = str(row[2]) d = str(row[3]) nic_list = [a,b,c,d] for j in list(host.split(',')): if j == nic_list[0] : os.chdir(report_dir) os.chdir(j) print nic_list db = rrdtool.updatev('nic.rrd','%s:%s:%s' % (nic_list[1],nic_list[2], nic_list[3])) time.sleep(1) print db print os.getcwd() print "nic.rrd"
def update_rrd(rrd_target): curl_dir = os.path.split(os.path.realpath(__file__))[0] rrd_dir = os.path.join(curl_dir, "static/data", rrd_target, "rrd") rrdfile = "{}/{}.rrd".format(rrd_dir, rrd_target) while True: start_time = time.time() get_result_by_fping = fping(rrd_target) r_loss = get_result_by_fping[0] print("{} loss is: {}".format(rrd_target, r_loss)) if r_loss == 100: r_time = 0 else: r_time = get_median(get_result_by_fping[1]) exec_update_rrd = rrdtool.updatev("{}".format(rrdfile), "N:-{}:{}".format(r_loss, r_time)) try: exec_update_rrd print("{}Update successful.Loss:{}\t Time:{}".format( rrd_target, r_loss, r_time)) except: print("{}Update fail.".format(rrd_target)) # ''' if r_loss: tr_save_file = my_tr(rrd_target) send_alarm_mail(rrd_target, r_loss, tr_save_file) wechat_subject = "[Alarm]{} Loss {}".format(rrd_target, r_loss) with open(tr_save_file) as f: wechat_content = f.read() wechat.send_to_wechat(wechat_subject, wechat_content) # ''' end_time = time.time() print(60 - int(end_time - start_time)) time.sleep(60 - int(end_time - start_time))
def update(): total_input_traffic = psutil.net_io_counters()[1] #获取网卡入流量 total_output_traffic = psutil.net_io_counters()[0] #获取网卡出流量 starttime = int(time.time()) update = rrdtool.updatev( '/home/test/rrdtool/Flow.rrd', '%s:%s:%s' % (str(starttime), str(total_input_traffic), str(total_output_traffic))) print(update)
def update(self, rrd_update_time, total_memory, available_memory): ''' 接收数据更新memory user rrd数据库 ''' update = rrdtool.updatev(self.rrd_name, '%s:%s:%s' % (str(rrd_update_time), str(available_memory), str(total_memory))) if update: return rrdtool.error()
def update_rrd(): total_in = psutil.net_io_counters()[1] total_out = psutil.net_io_counters()[0] starttime = int(time.time()) update = rrdtool.updatev('/usr/local/src/Flow.rrd','%s:%s:%s') % (str(starttime),str(total_in),str(total_out)) print update
def update(self, rrd_update_time, hardfree, hardtotal): ''' 接收数据更新hard disk rrd数据库 ''' update = rrdtool.updatev(self.rrd_name, '%s:%s:%s' % (str(rrd_update_time), str(hardfree), str(hardtotal))) if update: return rrdtool.error()
def update(self, rrd_update_time, input_traffic, output_traffic): ''' 接收数据更新网卡rrd数据库 ''' update = rrdtool.updatev(self.rrd_name, '%s:%s:%s' % (str(rrd_update_time), str(input_traffic), str(output_traffic))) if update: return rrdtool.error()
def rrdtool_insert2(self): for keys in psutil.network_io_counters(pernic=True): if keys == 'eth0': sent=psutil.network_io_counters(pernic=True)[keys][0] recv=psutil.network_io_counters(pernic=True)[keys][1] #up=rrdtool.updatev('rest.rrd','N:%d:%d' % (sent,recv)) up=rrdtool.updatev(self.rrd_file, 'N:%d:%d' % (recv,sent)) # print up print "sent: %f recv: %f" % (sent, recv)
def update(self, rrd_update_time, cpuloadone, cpuloadfive, cpuloadfifteen): ''' 接收数据更新cpu load rrd数据库 ''' update = rrdtool.updatev(self.rrd_name, '%s:%s:%s:%s' % (str(rrd_update_time), str(cpuloadone), str(cpuloadfive), str(cpuloadfifteen))) if update: return rrdtool.error()
def runMonitor(id, url, code, returnMsg): b = businessMonitor(url, code, returnMsg) data = b.runMonitor() curTime = str(int(time.time())) rrdPath = os.path.dirname( os.path.realpath(__file__)) + "/rrd/businessMonitoring_" + id + ".rrd" #update rrd namelookupTime = str(round(data["namelookupTime"], 2)) startTransferTime = str(round(data["startTransferTime"], 2)) totalTime = str(round(data["totalTime"], 2)) connectTime = str(round(data["connectTime"], 2)) if data["httpCode"] == 200 or data["httpCode"] == 302: try: rrdtool.updatev( rrdPath, "%s:%s:%s:%s:%s" % (curTime, namelookupTime, startTransferTime, totalTime, connectTime)) except Exception, e: print str(e)
def update(self,rrdfile, GCT_avg, S1_max, S1_ratio, Old_max, Heap_max, YGCT_avg, FGCT_avg, FGC, Metadata_used, Heap_used, Eden_max, Old_used, Eden_used, YGC, YGCT, Eden_ratio, S0_used, Metadata_max, FGCT, Old_ratio, Heap_ratio, S0_ratio, S1_used, S0_max, Metadata_ratio, GCT): #rrdtool update pf_stats_db.rrd --template BytesIn:BytesOut:PktsInPass:PktsInBlock:PktsOutPass:PktsOutBlock:States:StateSearchs:StateInserts:StateRemovals N:$RETURN_VALUE self.update = rrdtool.updatev(self.rrdfile, '--template', 'GCT_avg:S1_max:S1_ratio:Old_max:Heap_max:YGCT_avg:FGCT_avg:FGC:Metadata_used:Heap_used:Eden_max:Old_used:Eden_used:YGC:YGCT:Eden_ratio:S0_used:Metadata_max:FGCT:Old_ratio:Heap_ratio:S0_ratio:S1_used:S0_max:Metadata_ratio:GCT', '%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d' % (time.time(),GCT_avg, S1_max, S1_ratio, Old_max, Heap_max, YGCT_avg, FGCT_avg, FGC, Metadata_used, Heap_used, Eden_max, Old_used, Eden_used, YGC, YGCT, Eden_ratio, S0_used, Metadata_max, FGCT, Old_ratio, Heap_ratio, S0_ratio, S1_used, S0_max, Metadata_ratio, GCT)) print GCT_avg, S1_max, S1_ratio, Old_max, Heap_max, YGCT_avg, FGCT_avg, FGC, Metadata_used, Heap_used, Eden_max, Old_used, Eden_used, YGC, YGCT, Eden_ratio, S0_used, Metadata_max, FGCT, Old_ratio, Heap_ratio, S0_ratio, S1_used, S0_max, Metadata_ratio, GCT return self.update
def update(self, rrd_update_time,cpu_idle,cpu_interrupt, cpu_iowait, cpu_nice, cpu_softirg, cpu_steal, cpu_system, cpu_user): ''' 接收数据更新CPUutilization rrd数据库 特定参数,依据DS源而定 cpu_idle、cpu_interrupt、cpu_iowait、cpu_nice、cpu_softirg、cpu_steal、cpu_system、cpu_user ''' update = rrdtool.updatev(self.rrd_name, '%s:%s:%s:%s:%s:%s:%s:%s:%s' % (str(rrd_update_time), str(cpu_idle), str(cpu_interrupt), str(cpu_iowait), str(cpu_nice), str(cpu_softirg), str(cpu_steal), str(cpu_system), str(cpu_user))) if update: return rrdtool.error()
def update(self,host,port,rrdPath): proxy = JmxClient((host,port)) proxy = JmxClient((host,port)) resp = proxy.read("Catalina:type=ThreadPool,name=\"http-nio-"+port+"\"",["maxThreads","currentThreadsBusy","currentThreadCount","connectionCount"]) now = int(time.time()) maxThreads = resp.value["maxThreads"] currentThreadsBusy = resp.value["currentThreadsBusy"] currentThreadCount = resp.value["currentThreadCount"] connectionCount = resp.value["connectionCount"] update=rrdtool.updatev(rrdPath,'%s:%s:%s:%s:%s' % (str(now),str(maxThreads),str(currentThreadsBusy),str(currentThreadCount),str(connectionCount))) print update
def _rrdtool_insert(self, monitor_item, timestamp, value): ''' Update rrd db @param monitor_item monitor item in monitor_list @param timestamp int timestamp @param value the value under the timestamp ''' rrd_file = monitor_item + '.rrd' rlt = rrdtool.updatev(rrd_file, '%d:%d' %(timestamp, value)) if 'return_value' not in rlt: return {'message': 'rrdtool insert db error', 'code': -1} return {'message': 'rrdtool update db OK!', 'code': 0}
def updateRRD(self, rowobj): if str(rowobj["HTTP_CODE"]) == "200": unavailablevalue = 0 else: unavailablevalue = 1 FID = rowobj["FID"] time_rrdpath = RRDPATH + '/' + str( self.getURL(FID)) + '/' + str(FID) + '_' + str( self.rrdfiletype[0]) + '.rrd' download_rrdpath = RRDPATH + '/' + str( self.getURL(FID)) + '/' + str(FID) + '_' + str( self.rrdfiletype[1]) + '.rrd' unavailable_rrdpath = RRDPATH + '/' + str( self.getURL(FID)) + '/' + str(FID) + '_' + str( self.rrdfiletype[2]) + '.rrd' try: rrdtool.updatev( time_rrdpath, '%s:%s:%s:%s:%s:%s' % (str(rowobj["DATETIME"]), str(rowobj["NAMELOOKUP_TIME"]), str(rowobj["CONNECT_TIME"]), str(rowobj["PRETRANSFER_TIME"]), str(rowobj["STARTTRANSFER_TIME"]), str(rowobj["TOTAL_TIME"]))) rrdtool.updatev( download_rrdpath, '%s:%s' % (str(rowobj["DATETIME"]), str(rowobj["SPEED_DOWNLOAD"]))) rrdtool.updatev( unavailable_rrdpath, '%s:%s' % (str(rowobj["DATETIME"]), str(unavailablevalue))) logger.debug(rrdtool.last(time_rrdpath)) self.setMARK(rowobj["ID"]) except Exception, e: logger.error('Update rrd error:' + str(e))
def updateRRD(self, rowobj): if str(rowobj[7]) == "200": unavailablevalue = 0 else: unavailablevalue = 1 time_rrdpath = RRDPATH + '/' + str(self.getURL(rowobj[1])) + '/' + str( rowobj[1]) + '_' + str(self.rrdfiletype[0]) + '.rrd' download_rrdpath = RRDPATH + '/' + str(self.getURL( rowobj[1])) + '/' + str(rowobj[1]) + '_' + str( self.rrdfiletype[1]) + '.rrd' unavailable_rrdpath = RRDPATH + '/' + str(self.getURL( rowobj[1])) + '/' + str(rowobj[1]) + '_' + str( self.rrdfiletype[2]) + '.rrd' try: rrdtool.updatev( time_rrdpath, '%s:%s:%s:%s:%s:%s' % (str(rowobj[9]), str(rowobj[2]), str(rowobj[3]), str( rowobj[4]), str(rowobj[5]), str(rowobj[6]))) rrdtool.updatev(download_rrdpath, '%s:%s' % (str(rowobj[8]), str(rowobj[9]))) rrdtool.updatev(unavailable_rrdpath, '%s:%s' % (str(unavailablevalue), str(rowobj[9]))) self.setMARK(rowobj[0]) except Exception, e: logging.error('Update rrd error:' + str(e))
def update_rrd_file(): # 把本方法的脚本加入到crontab,并配置5分钟作为采集频率,crontab -e配置如下: # */5 * * * * /usr/bin/python3.6 /root/automation/rrdtool_update.py > /dev/null 2>&1 # 获取网卡入流量 total_input_traffic = psutil.net_io_counters()[1] # 获取网卡出流量 total_output_traffic = psutil.net_io_counters()[0] # 获取当前Linux时间戳 starttime = int(time.time()) # 将获取到的三个数据作为updatev的参数,返回{'return_value':0L}则说明更新成功,反之失败 update = rrdtool.updatev( 'Flow.rrd', '{0}:{1}:{2}'.format(str(starttime), str(total_input_traffic), str(total_output_traffic))) print(update)
def update(self,host,port,rrdPath): proxy = JmxClient((host,port)) resp = proxy.read("java.lang:type=MemoryPool,name=PS Eden Space",["Usage"]) eden_space = resp.value.get("Usage")["used"] resp = proxy.read("java.lang:type=MemoryPool,name=PS Survivor Space",["Usage"]) survivor_sapce = resp.value.get("Usage")["used"] resp = proxy.read("java.lang:type=MemoryPool,name=PS Old Gen",["Usage"]) old_gen = resp.value.get("Usage")["used"] resp = proxy.read("java.lang:type=MemoryPool,name=Code Cache",["Usage"]) code_cache = resp.value.get("Usage")["used"] resp = proxy.read("java.lang:type=MemoryPool,name=PS Perm Gen",["Usage"]) perm_gen = resp.value.get("Usage")["used"] now = int(time.time()) update=rrdtool.updatev(rrdPath,'%s:%s:%s:%s:%s:%s' % (str(now),str(eden_space),str(survivor_sapce),str(old_gen),str(code_cache),str(perm_gen))) print update
def mem_rrd(self, sql_mem): # mysql configure host_sql = self.host user = self.user password = self.password defaultdb = self.defaultdb # rrdtool host configure host = self.rrdtool_host report_dir = self.rrdtool_dir conn = MySQLdb.connect(host_sql, user, password, defaultdb) cur = conn.cursor() mem_list = [] cur.execute(sql_mem) rows = int(cur.rowcount) for i in xrange(rows): row = cur.fetchone() #print row[0],row[1],row[2] a = str(row[0]) m = str(row[1]) timeArray = time.strptime(m, "%Y-%m-%d %H:%M:%S") b = str(int(time.mktime(timeArray))) c = str(row[2]) d = str(row[3]) e = str(row[4]) f = str(row[5]) g = str(row[6]) h = str(row[7]) k = str(row[8]) l = str(row[9]) mem_list = [a, b, c, d, e, f, g, h, k, l] #print mem_list for j in list(host.split(',')): if j == mem_list[0]: #print "%s is %s" %(j,mem_list[0]) os.chdir(report_dir) os.chdir(j) #print os.getcwd() db = rrdtool.updatev( 'mem.rrd', '%s:%s:%s:%s:%s:%s:%s:%s:%s' % (mem_list[1], mem_list[2], mem_list[3], mem_list[4], mem_list[5], mem_list[6], mem_list[7], mem_list[8], mem_list[9])) print db time.sleep(1) print os.getcwd() print "mem.rrd"
def rrd_update(stats, value, interval, ds_type): rrd_name = '%s.rrd' % stats interval = str(interval) interval_mins = float(interval) / 60 heartbeat = str(int(interval) * 20) value = value if not os.path.exists(rrd_name): rrdb = rrdtool.create(rrd_name, '--step', '%s' % interval, 'DS:ds:%s:%s:0:U' %(ds_type, heartbeat), 'RRA:LAST:0.5:1:600', 'RRA:AVERAGE:0.5:5:600', 'RRA:MAX:0.5:5:600', 'RRA:MIN:0.5:5:600') if rrdb: print rrdtool.error() else: up = rrdtool.updatev(rrd_name, 'N:%d' % value) print time.strftime('%Y/%m/%d %H:%M:%S', time.localtime()), stats, value
def total_update(row, lock): node = row[0] device = row[1] ip = row[2] ifoid = row[3] flow = total_flow(ip, ifoid) filename = 'uplinkflow/%s/%s_total.rrd' %(row[0], row[1]) starttime=int (time.time ()) curtime = time.strftime('%Y-%m-%d %X', time.localtime()) if flow[0] != '' or flow[1] !='': update = rrdtool.updatev (str(filename), '%s:%s:%s' % (str (starttime), str(flow[0]), str(flow[1]))) with lock: log = file('../log/rrdupdate.log', 'a+') if update['return_value'] == 0: log.write('updated success %s %s %s in:%s out:%s\n' % (curtime, starttime, device, str(flow[0]), str(flow[1]))) else: log.write('updated fail %s %s %s in:%s out:%s\n' % (curtime, starttime, device, str(flow[0]), str(flow[1]))) log.close()
def mem_rrd(self,sql_mem): # mysql configure host_sql = self.host user = self.user password = self.password defaultdb = self.defaultdb # rrdtool host configure host = self.rrdtool_host report_dir = self.rrdtool_dir conn = MySQLdb.connect(host_sql,user,password,defaultdb) cur = conn.cursor() mem_list = [] cur.execute(sql_mem) rows = int(cur.rowcount) for i in xrange(rows): row = cur.fetchone() #print row[0],row[1],row[2] a = str(row[0]) m = str(row[1]) timeArray = time.strptime(m, "%Y-%m-%d %H:%M:%S") b = str(int(time.mktime(timeArray))) c = str(row[2]) d = str(row[3]) e = str(row[4]) f = str(row[5]) g = str(row[6]) h = str(row[7]) k = str(row[8]) l = str(row[9]) mem_list = [a,b,c,d,e,f,g,h,k,l] #print mem_list for j in list(host.split(',')): if j == mem_list[0] : #print "%s is %s" %(j,mem_list[0]) os.chdir(report_dir) os.chdir(j) #print os.getcwd() db = rrdtool.updatev('mem.rrd','%s:%s:%s:%s:%s:%s:%s:%s:%s' % (mem_list[1], mem_list[2],mem_list[3],mem_list[4],mem_list[5],mem_list[6], mem_list[7],mem_list[8],mem_list[9])) print db time.sleep(1) print os.getcwd() print "mem.rrd"
def updateRRD(self,rowobj): if str(rowobj[7])=="200": unavailablevalue=0 else: unavailablevalue=1 time_rrdpath=RRDPATH+'/'+str(self.getURL(rowobj[1]))+'/'+str(rowobj[1])+'_'+str(self.rrdfiletype[0])+'.rrd' download_rrdpath=RRDPATH+'/'+str(self.getURL(rowobj[1]))+'/'+str(rowobj[1])+'_'+str(self.rrdfiletype[1])+'.rrd' unavailable_rrdpath=RRDPATH+'/'+str(self.getURL(rowobj[1]))+'/'+str(rowobj[1])+'_'+str(self.rrdfiletype[2])+'.rrd' try: rrdtool.updatev(time_rrdpath,'%s:%s:%s:%s:%s:%s' % (str(rowobj[9]),str(rowobj[2]),str(rowobj[3]),str(rowobj[4]),str(rowobj[5]),str(rowobj[6]))) rrdtool.updatev(download_rrdpath,'%s:%s' % (str(rowobj[8]),str(rowobj[9]))) rrdtool.updatev(unavailable_rrdpath,'%s:%s' % (str(unavailablevalue),str(rowobj[9]))) self.setMARK(rowobj[0]) except Exception,e: logging.error('Update rrd error:'+str(e))
def total_update(row, lock): node = row[0] device = row[1] ip = row[2] ifoid = row[3] flow = total_flow(ip, ifoid) filename = 'uplinkflow/%s/%s_total.rrd' % (row[0], row[1]) starttime = int(time.time()) curtime = time.strftime('%Y-%m-%d %X', time.localtime()) if flow[0] != '' or flow[1] != '': update = rrdtool.updatev( str(filename), '%s:%s:%s' % (str(starttime), str(flow[0]), str(flow[1]))) with lock: log = file('../log/rrdupdate.log', 'a+') if update['return_value'] == 0: log.write('updated success %s %s %s in:%s out:%s\n' % (curtime, starttime, device, str(flow[0]), str(flow[1]))) else: log.write('updated fail %s %s %s in:%s out:%s\n' % (curtime, starttime, device, str(flow[0]), str(flow[1]))) log.close()
def cpu_rrd(self, sql_cpu): # mysql configure host_sql = self.host user = self.user password = self.password defaultdb = self.defaultdb # rrdtool host configure host = self.rrdtool_host report_dir = self.rrdtool_dir conn = MySQLdb.connect(host_sql, user, password, defaultdb) cur = conn.cursor() cpu_percent = [] cur.execute(sql_cpu) rows = int(cur.rowcount) for i in xrange(rows): row = cur.fetchone() #print row[0],row[1],row[2] a = str(row[0]) e = str(row[1]) timeArray = time.strptime(e, "%Y-%m-%d %H:%M:%S") b = str(int(time.mktime(timeArray))) c = str(row[2]) cpu_percent = [a, b, c] #print cpu_percent[0] for j in list(host.split(',')): #print cpu_percent[1],cpu_percent[2] if j == cpu_percent[0]: # print report_dir os.chdir(report_dir) os.chdir(j) # print type(cpu_percent[1]) # print type(cpu_percent[2]) db = rrdtool.updatev( 'cpu.rrd', '%s:%s' % (cpu_percent[1], cpu_percent[2])) print db time.sleep(5) print os.getcwd() print "cpu.rrd"
def disk_rrd(self, sql_disk): # mysql configure host_sql = self.host user = self.user password = self.password defaultdb = self.defaultdb # rrdtool host configure host = self.rrdtool_host report_dir = self.rrdtool_dir conn = MySQLdb.connect(host_sql, user, password, defaultdb) cur = conn.cursor() disk_list = [] cur.execute(sql_disk) rows = int(cur.rowcount) for i in xrange(rows): row = cur.fetchone() #print row[0],row[1],row[2] a = str(row[0]) g = str(row[1]) timeArray = time.strptime(g, "%Y-%m-%d %H:%M:%S") b = str(int(time.mktime(timeArray))) c = str(row[2]) d = str(row[3]) e = str(row[4]) f = str(row[5]) disk_list = [a, b, c, d, e, f] for j in list(host.split(',')): if j == disk_list[0]: os.chdir(report_dir) os.chdir(j) db = rrdtool.updatev( 'disk.rrd', '%s:%s:%s:%s:%s' % (disk_list[1], disk_list[2], disk_list[3], disk_list[4], disk_list[5])) print db time.sleep(1) print os.getcwd() print "disk.rrd"
def rrd_update(self): if not os.path.exists(self.getRrdFilename()): self.rrd_create() #create the list of update arguments update_args = [] #the rrd target file update_args.append(self.getRrdFilename()) # magnitude quantities with "%f"-formatstring will only # result in the value if self.inptype == "cnt": snmpValList = self.getRawSnmpValues() arg_string = "%f" % time.time() for snmpVal in self.getRawSnmpValues(): arg_string += ":%d" % int(snmpVal) update_args.append(arg_string) else: snmpValList = self.getRawSnmpValues() arg_string = "%f" % time.time() for snmpVal in self.getRawSnmpValues(): arg_string += ":%f" % snmpVal update_args.append(arg_string) return rrdtool.updatev(*update_args)
def updateRRD(self,rowobj): if str(rowobj["HTTP_CODE"])=="200": unavailablevalue=0 else: unavailablevalue=1 FID=rowobj["FID"] time_rrdpath=RRDPATH+'/'+str(self.getURL(FID))+'/'+str(FID)+'_'+str(self.rrdfiletype[0])+'.rrd' download_rrdpath=RRDPATH+'/'+str(self.getURL(FID))+'/'+str(FID)+'_'+str(self.rrdfiletype[1])+'.rrd' unavailable_rrdpath=RRDPATH+'/'+str(self.getURL(FID))+'/'+str(FID)+'_'+str(self.rrdfiletype[2])+'.rrd' try: rrdtool.updatev(time_rrdpath,'%s:%s:%s:%s:%s:%s' % (str(rowobj["DATETIME"]),str(rowobj["NAMELOOKUP_TIME"]),str(rowobj["CONNECT_TIME"]),str(rowobj["PRETRANSFER_TIME"]),str(rowobj["STARTTRANSFER_TIME"]),str(rowobj["TOTAL_TIME"]))) rrdtool.updatev(download_rrdpath,'%s:%s' % (str(rowobj["DATETIME"]),str(rowobj["SPEED_DOWNLOAD"]))) rrdtool.updatev(unavailable_rrdpath,'%s:%s' % (str(rowobj["DATETIME"]),str(unavailablevalue))) self.setMARK(rowobj["ID"]) except Exception,e: logging.error('Update rrd error:'+str(e))
def cpu_rrd(self,sql_cpu): # mysql configure host_sql = self.host user = self.user password = self.password defaultdb = self.defaultdb # rrdtool host configure host = self.rrdtool_host report_dir = self.rrdtool_dir conn = MySQLdb.connect(host_sql,user,password,defaultdb) cur = conn.cursor() cpu_percent = [] cur.execute(sql_cpu) rows = int(cur.rowcount) for i in xrange(rows): row = cur.fetchone() #print row[0],row[1],row[2] a = str(row[0]) e = str(row[1]) timeArray = time.strptime(e, "%Y-%m-%d %H:%M:%S") b = str(int(time.mktime(timeArray))) c = str(row[2]) cpu_percent = [a,b,c] #print cpu_percent[0] for j in list(host.split(',')): #print cpu_percent[1],cpu_percent[2] if j == cpu_percent[0] : # print report_dir os.chdir(report_dir) os.chdir(j) # print type(cpu_percent[1]) # print type(cpu_percent[2]) db = rrdtool.updatev('cpu.rrd','%s:%s' % (cpu_percent[1],cpu_percent[2])) print db time.sleep(5) print os.getcwd() print "cpu.rrd"
import messages import vw_conf import time import datetime import rrdtool dbpath = '/home/pi/pwm/watering2-pi/moisture.rrd' pin = 0 r = messages.send_and_receive(messages.analog_read_req(pin)) readings = "N:" + "%.2f:%.2f:%.2f" % (r.to_ohm( r.value - r.sigma), r.to_ohm(r.value), r.to_ohm(r.value + r.sigma)) print(readings) rrdtool.updatev([dbpath, readings])
# -*- coding: utf-8 -*- #!/usr/bin/python import rrdtool import time,psutil total_input_traffic = psutil.net_io_counters()[1] total_output_traffic = psutil.net_io_counters()[0] starttime=int(time.time()) update=rrdtool.updatev('/home/test/rrdtool/Flow.rrd','%s:%s:%s' % (str(starttime),str(total_input_traffic),str(total_output_traffic))) print update
em2_bytes_sent = str(row[3]) em2_bytes_recv = str(row[4]) em3_bytes_sent = str(row[5]) em3_bytes_recv = str(row[6]) em4_bytes_sent = str(row[7]) em4_bytes_recv = str(row[8]) eth0_bytes_sent = str(row[9]) eth0_bytes_recv = str(row[10]) eth1_bytes_sent = str(row[11]) eth1_bytes_recv = str(row[12]) if em1_bytes_sent == 'None': em1_bytes_sent = '0' em1_bytes_recv = '0' em2_bytes_sent = '0' em2_bytes_recv = '0' update = rrdtool.updatev('../rrddate/%s_wflow.rrd' % ip,'%s:%s:%s:%s:%s' % (starttime,em1_bytes_sent,em1_bytes_recv,eth0_bytes_sent,eth0_bytes_recv)) update1 = rrdtool.updatev('../rrddate/%s_nflow.rrd' % ip,'%s:%s:%s:%s:%s' % (starttime,em2_bytes_sent,em2_bytes_recv,eth1_bytes_sent,eth1_bytes_recv)) print update print update1 else: eth0_bytes_sent = '0' eth0_bytes_recv = '0' eth1_bytes_sent = '0' eth1_bytes_recv = '0' update = rrdtool.updatev('../rrddate/%s_wflow.rrd' % ip,'%s:%s:%s:%s:%s' % (starttime,em1_bytes_sent,em1_bytes_recv,eth0_bytes_sent,eth0_bytes_recv)) update1 = rrdtool.updatev('../rrddate/%s_nflow.rrd' % ip,'%s:%s:%s:%s:%s' % (starttime,em2_bytes_sent,em2_bytes_recv,eth1_bytes_sent,eth1_bytes_recv)) print update print update1 except Exception,e: print e db.close()
def rrd_update(): total_input_traffic = psutil.net_io_counters()[1] total_output_traffic = psutil.net_io_counters()[0] starttime=int(time.time()) update=rrdtool.updatev('/root/net_flowes.rrd','%s:%s:%s' % (str(starttime),str(total_input_traffic),str(total_output_traffic))) print update
def save_to_pm2_rrd(dat) : rrdtool.updatev(RRD_PM2_DB,'N:%d' % (dat))
#!coding=utf-8 import MySQLdb import rrdtool,time db = MySQLdb.connect("127.0.0.1","root","QX9rX8UY50gznYo","kypform" ) cursor = db.cursor() sql='select ip,five_load,ten_load,ft_load from monitor_host_status;' starttime=str(int(time.time())) try: cursor.execute(sql) results = cursor.fetchall() for row in results: ip = row[0] five_load = str(float(row[1])) ten_load = str(float(row[2])) ft_load = str(float(row[3])) update = rrdtool.updatev('../rrddate/%s_load.rrd' % ip,'%s:%s:%s:%s' % (starttime,five_load,ten_load,ft_load)) print update except Exception,e: print e db.close()
def update_rrd(rrd_name, time, data1): update = rrdtool.updatev(rrd_name, '%s:%s' % (str(starttime), str(data1)))
def rrd_update(rrdname, rx): start_time = int(time.time()) print(rrdname, start_time, type(start_time), rx, type(rx)) x = rrdtool.updatev(rrdname, "%s:%s" % (str(start_time), str(rx))) if x: print(x)
if args.verbose: print "Directory created!" else: if args.verbose: print "Directory already exists." if args.verbose: print "RRD file path: %s" %(rrd_file) # Creating database (rrd), if not exist else update(v) if not os.path.exists(rrd_file): if args.verbose: print "Creating rrd file now...", rrdtool.create(str(rrd_file), '--step', '300', '--no-overwrite', 'DS:ifInOctets:COUNTER:900:0:18446744073709551615', 'DS:ifOutOctets:COUNTER:900:0:18446744073709551615', 'DS:ifOperStatus:GAUGE:900:0:100', 'RRA:MAX:0.5:1:288', 'RRA:MIN:0.5:1:288', 'RRA:AVERAGE:0.5:1:288', 'RRA:AVERAGE:0.5:6:336', 'RRA:AVERAGE:0.5:24:360', 'RRA:AVERAGE:0.5:288:365') if args.verbose: print "created!" else: # Command: rrdtool update(v) network_usage_db.rrd --template in-trafic:out-trafic N:10:20 if args.verbose: print "RRD already exists." return_val = rrdtool.updatev(str(rrd_file), 'N:%s:%s:%s' %(args.in_traffic, args.out_traffic, args.oper_status)) print return_val else: return_val = rrdtool.update(str(rrd_file), 'N:%s:%s:%s' %(args.in_traffic, args.out_traffic, args.oper_status))
#!/usr/bin/env python import rrdtool import time,psutil #total_input_traffic = psutil.net_io_counters()[1] total_input_traffic = psutil.network_io_counters()[1] #total_output_traffic = psutil.net_io_counters()[0] total_output_traffic = psutil.network_io_counters()[0] starttime=int(time.time()) update=rrdtool.updatev('/var/www/vhosts/rrdtool/Flow.rrd','%s:%s:%s' % (str(starttime),str(total_input_traffic),str(total_output_traffic))) print update
w.write(log + '\n') else: print "Did not find new log file." if __name__ == '__main__': format_log() count = [] with open('/root/mcr/testtmp/' + ungzip_name, 'r') as f: return_code = ['502', '500', '404', '403', '000'] log_code = [] lines = f.readlines() for line in lines: code = line.split()[3].split('/')[1] log_code.append(code) for i in return_code: result = log_code.count(i) count.append(result) code_502 = count[0] code_500 = count[1] code_403 = count[2] code_404 = count[3] code_000 = count[4] starttime = int(time.time()) update = rrdtool.updatev( '/root/mcr/xls/Flow.rrd', '%s:%s:%s:%s:%s:%s' % (str(starttime), str(code_502), str(code_500), str(code_404), str(code_403), str(code_000))) print update
#-*- coding=utf-8 -*- #!/usr/bin/python #author: [email protected] import rrdtool import time, psutil, os rrd_path = os.path.join(os.getcwd(), "Flow.rrd") total_input_trafic = 103334355 #psutil.net_io_counters()[1] total_ouput_trafic = 111111 #psutil.net_io_counters()[0] start_time = int(time.time()) update = rrdtool.updatev( rrd_path, "%s:%s:%s" % (str(start_time), str(total_input_trafic), str(total_ouput_trafic))) print update
# -*- coding: utf-8 -*- import rrdtool import time, psutil total_input_traffic = psutil.net_io_counters()[1] total_output_traffic = psutil.net_io_counters()[0] starttime = int(time.time()) update = rrdtool.updatev( 'Flow.rrd', '%s:%s:%s' % (str(starttime), str(total_input_traffic), str(total_output_traffic))) print(update, total_input_traffic, total_output_traffic)
#!coding=utf-8 import MySQLdb import rrdtool,time db = MySQLdb.connect("127.0.0.1","root","QX9rX8UY50gznYo","kypform" ) cursor = db.cursor() sql='select ip,cpu_user,cpu_nice,cpu_system,cpu_idle,cpu_iowait,cpu_irq,cpu_softirq from monitor_host_status;' starttime=str(int(time.time())) try: cursor.execute(sql) results = cursor.fetchall() for row in results: ip = row[0] cpu_user = str(float(row[1])) cpu_nice = str(float(row[2])) cpu_system = str(float(row[3])) cpu_idle = str(float(row[4])) cpu_iowait = str(float(row[5])) cpu_irq = str(float(row[6])) cpu_softirq = str(float(row[7])) update = rrdtool.updatev('../rrddate/%s_cpu.rrd' % ip,'%s:%s:%s:%s:%s:%s:%s:%s' % (starttime,cpu_user,cpu_nice,cpu_system,cpu_idle,cpu_iowait,cpu_irq,cpu_softirq)) print update except Exception,e: print e db.close()
def save_to_rrd(dat) : (temp, hum) = dat rrdtool.updatev(RRD_DB,'N:%d:%d' % (temp, hum))
def insert_rrd(self): values_str = "N:" + ":".join(map(str, map(self.values.get, sorted(self.values.keys())))) log.debug(values_str) rrdtool.updatev(self.path, values_str)
#-*- coding=utf-8 -*- #!/usr/bin/python #author: [email protected] import rrdtool import time, psutil,os rrd_path = os.path.join(os.getcwd(), "Flow.rrd") total_input_trafic = 103334355 #psutil.net_io_counters()[1] total_ouput_trafic = 111111 #psutil.net_io_counters()[0] start_time = int(time.time()) update = rrdtool.updatev(rrd_path, "%s:%s:%s"%(str(start_time), str(total_input_trafic), str(total_ouput_trafic))) print update
#!coding=utf-8 import MySQLdb import rrdtool,time db = MySQLdb.connect("127.0.0.1","root","QX9rX8UY50gznYo","kypform" ) cursor = db.cursor() sql='select ip,diskusage2 from monitor_host_status;' starttime=str(int(time.time())) try: cursor.execute(sql) results = cursor.fetchall() for row in results: ip = row[0] diskusage2 = str(float(row[1])) update = rrdtool.updatev('../rrddate/%s_disk2.rrd' % ip,'%s:%s' % (starttime,diskusage2)) print update except Exception,e: print e db.close()
'DS:output:GAUGE:120:U:U', 'RRA:LAST:0.5:1:600', 'RRA:AVERAGE:0.5:5:600', 'RRA:MAX:0.5:5:600', 'RRA:MIN:0.5:5:600') if rrdb: print rrdtool.error() # ##rrd插入数据 def insert_data() for keys in psutil.network_io_counters(pernic=True): if keys == 'em1': sent = psutil.network_io_counters(pernic=True)[keys][0] recv = psutil.network_io_counters(pernic=True)[keys][1] up = rrdtool.updatev('rest.rrd', 'N:%d:%d' % (sent, recv)) print up # ##根据rrd绘图 def draw_graph(): rrdtool.graph('rest.png', '--start', '1369983960', '--title', 'my rrd graph test', '--vertical-label', 'bits', 'DEF:input=rest.rrd:input:LAST', 'DEF:output=rest.rrd:output:LAST', 'LINE1:input#0000FF:In traffic', 'LINE1:output#00FF00:Out traffic\\r', 'CDEF:bytes_in=input,8,*', 'CDEF:bytes_out=output,8,*', 'COMMENT:\\n',
'RRA:MAX:0.5:24:775', 'RRA:MAX:0.5:28:797', 'RRA:MIN:0.5:1:600', 'RRA:MIN:0.5:6:700', 'RRA:MIN:0.5:24:775', 'RRA:MIN:0.5:28:797') if rrd: print(rrdtool.error()) # 2、采用updatev()方法更新数据库 # update filename [--template|-t ds-name[:ds-name]...] N|timestamp:value[:value...] [timestamp:value[:value...] ...] 方法, # 存储一个新值到rrdtool数据库,updatev和update类似,区别是每次插入后会返回一个状态码,以便了解是否成功(updatev用0表示成功,–1表示失败)。参数说明如下: # filename指定存储数据到的目标rrd文件名; # -t ds-name[:ds-name]指定需要更新的DS名称; # N|Timestamp表示数据采集的时间戳,N表示当前时间戳; # value[:value...]更新的数据值,多个DS则多个值。 total_input_traffic = psutil.net_io_counters()[1] # 网卡入流量 total_output_traffic = psutil.net_io_counters()[0] # 网卡出流量 start_time = int(time.time()) # 获取当前时间 # 将获取到的三个数据作为updatev的参数,返回{'return_value': 0L}则说明更新成功,反之失败 update = rrdtool.updatev( '/home/pycode/py自动化/Flow.rrd', '%s:%s:%s' % (str(start_time), str(total_input_traffic), str(total_output_traffic))) print(update) # [root@localhost rrdtool]# crontab -e # crontab: installing new crontab # [root@localhost rrdtool]# crontab -l # */5 * * * * /usr/bin/python /home/pycode/py自动化/322_rrdtool_网卡流量图绘制.py > /dev/null 2>&1
#!/usr/bin/env python #coding: utf-8 #实现网卡流量图表绘制 import rrdtool import time,psutil total_input_traffic = psutil.net_io_counters()[1] total_output_traffic = psutil.net_io_counters()[0] starttime = int(time.time()) update = rrdtool.updatev('/root/AutomationOPS/rrdtool/Flow.rrd','%s:%s:%s' % (str(starttime),str(total_input_traffic),str(total_output_traffic))) print update
'RRA:MAX:0.5:288:797', 'RRA:MIN:0.5:288:797') if rrdb: print rrdtool.error() ''' #time.sleep(5) # 插入数据 for keys in psutil.network_io_counters(pernic=True): if keys == 'eth3': sent=psutil.network_io_counters(pernic=True)[keys][0] recv=psutil.network_io_counters(pernic=True)[keys][1] #up=rrdtool.updatev('rest.rrd','N:%d:%d' % (sent,recv)) up=rrdtool.updatev('rest.rrd','N:%d:%d' % (recv,sent)) print up print "sent: %f recv: %f" % (sent, recv) def tabNum(num=13): tstr='\\t' * num return tstr def dtNowString(dt_format='%Y-%m-%d %H:%M:%S'): dt = datetime.now() res = dt.strftime(dt_format) if dt_format == '%Y-%m-%d': res = res + ' 00:00:00' rlt = str(res).replace(':', '\:')
def updateRRD(self, rowobj): print("==========================>>>updateRRD", rowobj.http_status) if str(rowobj.http_status) == "200": unavailablevalue = 0 else: unavailablevalue = 1 FID = rowobj.fid.id print("++++++++++++++++++++++++++++++++++++++") print(unavailablevalue) print(rowobj.fid.id) print("++++++++++++++++++++++++++++++++++++++") time_rrdpath = config.RRD_PATH + '/' + str( self.getURL(FID)) + '/' + str(FID) + '_' + str( self.rrdfiletype[0]) + '.rrd' download_rrdpath = config.RRD_PATH + '/' + str( self.getURL(FID)) + '/' + str(FID) + '_' + str( self.rrdfiletype[1]) + '.rrd' unavailable_rrdpath = config.RRD_PATH + '/' + str( self.getURL(FID)) + '/' + str(FID) + '_' + str( self.rrdfiletype[2]) + '.rrd' print(FID) print(time_rrdpath) print(download_rrdpath) print(unavailable_rrdpath) try: temp = rrdtool.updatev( time_rrdpath, '%s:%s:%s:%s:%s:%s' % (str(rowobj.datetime), str(rowobj.dns_lookup_time), str(rowobj.connect_time), str(rowobj.pre_transfer_time), str(rowobj.start_transfer_time), str(rowobj.total_time))) temp1 = rrdtool.updatev( download_rrdpath, '%s:%s' % (str(rowobj.datetime), str(rowobj.download_speed))) temp2 = rrdtool.updatev( unavailable_rrdpath, '%s:%s' % (str(rowobj.datetime), str(unavailablevalue))) self.setMARK(rowobj.id) print( "------------------------------------------------------------>>" ) print(temp) print(temp1) print(temp2) print( "------------------------------------------------------------>>" ) print(str(rowobj.datetime), str(rowobj.dns_lookup_time), str(rowobj.connect_time), str(rowobj.pre_transfer_time), str(rowobj.start_transfer_time), str(rowobj.total_time)) # print (rowobj.size_header) except Exception, e: logging.error('Update rrd error:' + str(e))
#!coding=utf-8 import MySQLdb import rrdtool,time db = MySQLdb.connect("127.0.0.1","root","QX9rX8UY50gznYo","kypform" ) cursor = db.cursor() sql='select ip,phymemusage,virtmemusage from monitor_host_status;' starttime=str(int(time.time())) try: cursor.execute(sql) results = cursor.fetchall() for row in results: ip = row[0] phymemusage = str(float(row[1])) virtmemusage = str(float(row[2])) update = rrdtool.updatev('../rrddate/%s_mem.rrd' % ip,'%s:%s:%s' % (starttime,phymemusage,virtmemusage)) print update except Exception,e: print e db.close()