def update(filename,value): ''' rrdtool update function ''' cmd = "%s update %s \ N:%s" %(rrdtool,filename,value) try: subprocess.call(cmd, shell=True) except: log.error('update rrd file %s failed' %(filename))
def update(filename, value): ''' rrdtool update function ''' cmd = "%s update %s \ N:%s" % (rrdtool, filename, value) try: subprocess.call(cmd, shell=True) except: log.error('update rrd file %s failed' % (filename))
def graph(filename,rrd_file,rrd_ds,rrd_color): ''' rrdtool graph function ''' cmd = "%s graph %s \ DEF:v1=%s:%s:AVERAGE \ AREA:v1#%s" %(rrdtool,filename,rrd_file,rrd_ds,rrd_color) try: subprocess.call(cmd, shell=True) except: log.error('graph rrd file %s failed' %(filename))
def ngyaml(filename): ''' 解析yaml配置文件 ''' d = {} try: f = open(filename) d = yaml.safe_load(f) except: log.error('can\'t open the yaml file %s' %(filename)) return d
def graph(filename, rrd_file, rrd_ds, rrd_color): ''' rrdtool graph function ''' cmd = "%s graph %s \ DEF:v1=%s:%s:AVERAGE \ AREA:v1#%s" % (rrdtool, filename, rrd_file, rrd_ds, rrd_color) try: subprocess.call(cmd, shell=True) except: log.error('graph rrd file %s failed' % (filename))
def daemonize(self): """ do the UNIX double-fork magic, see Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 """ try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: log.error("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1)
def create(filename,rrd_ds,rrd_dst,interval): ''' rrdtool create in python ''' start = int(time.time()) - 86400 step = interval * 60 cmd = "%s create %s \ --start %s --step %s \ DS:%s:%s:600:0:U \ RRA:LAST:0.5:1:600 RRA:LAST:0.5:6:700 RRA:LAST:0.5:24:775 RRA:LAST:0.5:288:797 \ RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797\ RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:444:797 \ RRA:MIN:0.5:1:600 RRA:MIN:0.5:6:700 RRA:MIN:0.5:24:775 RRA:MIN:0.5:444:797 " %(rrdtool,filename,start,step,rrd_ds,rrd_dst) try: subprocess.call(cmd, shell=True) except: log.error('create rrd file %s failed' %(filename))
def create(filename, rrd_ds, rrd_dst, interval): ''' rrdtool create in python ''' start = int(time.time()) - 86400 step = interval * 60 cmd = "%s create %s \ --start %s --step %s \ DS:%s:%s:600:0:U \ RRA:LAST:0.5:1:600 RRA:LAST:0.5:6:700 RRA:LAST:0.5:24:775 RRA:LAST:0.5:288:797 \ RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797\ RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:444:797 \ RRA:MIN:0.5:1:600 RRA:MIN:0.5:6:700 RRA:MIN:0.5:24:775 RRA:MIN:0.5:444:797 " % ( rrdtool, filename, start, step, rrd_ds, rrd_dst) try: subprocess.call(cmd, shell=True) except: log.error('create rrd file %s failed' % (filename))
def report_mysql(data): """ report collect data to mysql . need MySQL-python module note that: host is your mysql server ip or hostname user is your mysql user passwd is your mysql password database is the database store collect data data is a dict (note:is dict),the key is mysql table's key ,and the value of the dict is your collect data or other thing """ try: import MySQLdb except ImportError: log.error('MySQLdb module is not install in this server !') sys.exit(200) sql = 'insert into `' + table + '` (' keys = [] values = [] try: conn = MySQLdb.connect(nginit.MYSQL_HOST,nginit.MYSQL_USER,nginit.MYSQL_PASSWD) conn.select_db(nginit.MYSQL_DATABASE) cursor = conn.cursor() except: log.error('connect mysql server or database error !') if isinstance(data,dict): for (key,value) in data.items(): keys.append(key) values.append(value) for i in range(len(keys)): if i < len(keys) - 1 : sql += '`' + str(keys[i]) + '`' + ',' else: sql += '`' + str(keys[i]) + '` ) values (' for j in range(len(values)): if j < len(values) - 1: sql += '"' + str(values[j]) + '"' + ',' else: sql += '"' + str(values[j]) + '" )' else: log.error(' input data not a dict') try: cursor.execute(sql) conn.commit() log.info('report ' + str(data) + ' to ' + host + ' success') except: log.error('insert sql '+ sql +' error')
def report_mongo(data): """ report collect data to mongodb need pymongo library """ try: import pymongo except: log.error('pymongo library is not installed in this server') import pymongo #connect your mongo server conn = pymongo.MongoClient('mongodb://*****:*****@localhost:27017') #get or create a new database db = conn.ng_mini #check auth #db.authenticate(nginit.MONGO_USER,nginit.MONGO_PASSWD) #select a collecton (a table),assume your collection name is monitor collection = db.monitor if isinstance(data,dict): try: collection.insert(data) log.info('report ' + str(data) + 'to' + nginit.MONGO_HOST + ' success') except: log.error('report ' + str(data) + 'to' + nginit.MONGO_HOST + ' failed') else: log.error('data not a dict !')
class Daemon: """ A generic daemon class. Usage: subclass the Daemon class and override the run() method """ def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile def daemonize(self): """ do the UNIX double-fork magic, see Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 """ try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: log.error("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1) # decouple from parent environment os.chdir("/") os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent sys.exit(0) except OSError, e: log.error("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1)