
复制#!/usr/bin/python #to get webcdn totaol statistics # -*- coding: utf8 -*- import MySQLdb import sys import os def get_total_value(sql): db = MySQLdb.connect(host=xxxx,析脚
user=xxxx,passwd=xxxx,db=xxxx) cursor = db.cursor() cursor.execute(sql) try: result = cursor.fetchone()[0] except: result = 0 cursor.close() db.close() return result if __name__ == __main__: sql = if sys.argv[1] == "network_traffic": sql = "select round(sum(lastvalue)/(1024*1024),4) from hosts a, items b where key_ in ( net.if.out[eth1,bytes],net.if.out[eth0,bytes]) and lower(host) like %-cdn-cache% and a.hostid = b.hostid" elif sys.argv[1] == "nginx_traffic": sql = "select sum(lastvalue) from hosts a, items b where key_ = log_webcdn_getvalue[traffic] and lower(host) like %cdn-cache% and a.hostid = b.hostid" elif sys.argv[1] == "2xxand3xx": sql = "select sum(lastvalue) from hosts a, items b where key_ in ( log_webcdn_getvalue[200],log_webcdn_getvalue[300]) and lower(host) like %-cdn-cache% and a.hostid = b.hostid" elif sys.argv[1] == "4xxand5xx": sql = "select sum(lastvalue) from hosts a, items b where key_ in ( log_webcdn_getvalue[four],log_webcdn_getvalue[five]) and lower(host) like %-cdn-cache% and a.hostid = b.hostid" elif sys.argv[1] == "network_ss": sql = "select sum(lastvalue) from hosts a, items b where key_ = network_conn and lower(host) like %-cdn-cache% and a.hostid = b.hostid" else: sys.exit(0) # print sql value = get_total_value(sql) print value 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.