def sendmail(to, subject, body): session = smtp_conn(loadconfig('EMAIL','SMTP_SERVER'), int(loadconfig('EMAIL','PORT')), loadconfig('EMAIL','USER'), loadconfig('EMAIL','PASSWD')) if session: ret = sendMail(session, loadconfig('EMAIL','FROM'), to, subject, body) return ret else: log_error("smtp_conn():" + str('Email session Failure. Please check username and password!'))
def mysql_phandler(): MDB = MySQLdb.connect(host=loadconfig('MYSQL', 'HOST'), user=loadconfig('MYSQL', 'USER'), passwd=loadconfig('MYSQL', 'PASSWD'), db=loadconfig('MYSQL', 'DB'), port=int(loadconfig('MYSQL', 'PORT')), charset='utf8') return MDB
def log_error(errorinfo): if(loadconfig('LOG','ERROR') == '0'): return logdir = loadconfig('DIRS','PY_FLW_LOG') + time.strftime('%Y/%m/%d', time.localtime()) + '/' if not os.path.exists(logdir): os.system('mkdir -p ' + logdir) logfile = logdir + "flwinfo.log" f = open(logfile, 'a') f.write('ERROR ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ' ' + str(errorinfo) + '\n') f.close()
def sendMsg(phonenuber , text): import requests requestsurl = 'http://sdk2.entinfo.cn/z_send.aspx' payload = {'sn' : loadconfig('MSMID','SN'),'pwd' : loadconfig('MSMID','SENDER'), 'mobile' : phonenuber, 'content' : text.decode('utf8').encode('gbk')} ret = requests.get(requestsurl,params = payload ,timeout = 2 ) if ret.status_code == 200: if ret.text == '1': return True else: log_error("sendMsg(): " + str('Send MSM error') + ' err code: ' + str(ret.text)) return ret.text else: return False
def log_error(errorinfo): if (loadconfig('LOG', 'ERROR') == '0'): return logdir = loadconfig('DIRS', 'PY_FLW_LOG') + time.strftime( '%Y/%m/%d', time.localtime()) + '/' if not os.path.exists(logdir): os.system('mkdir -p ' + logdir) logfile = logdir + "flwinfo.log" f = open(logfile, 'a') f.write('ERROR ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ' ' + str(errorinfo) + '\n') f.close()
def DE(string, cryptkeys=loadconfig('CRYPTKEY', 'CRYPTKEY')): s = str2asc(string) data = [] for i in s: data.append(i ^ int(cryptkeys)) string = asc2str(data) return string
def DE(string, cryptkeys = loadconfig('CRYPTKEY','CRYPTKEY')): s = str2asc(string) data = [] for i in s: data.append(i ^ int(cryptkeys)) string = asc2str(data) return string
def sendMsg(phonenuber, text): import requests requestsurl = 'http://sdk2.entinfo.cn/z_send.aspx' payload = { 'sn': loadconfig('MSMID', 'SN'), 'pwd': loadconfig('MSMID', 'SENDER'), 'mobile': phonenuber, 'content': text.decode('utf8').encode('gbk') } ret = requests.get(requestsurl, params=payload, timeout=2) if ret.status_code == 200: if ret.text == '1': return True else: log_error("sendMsg(): " + str('Send MSM error') + ' err code: ' + str(ret.text)) return ret.text else: return False
class FlwDaemon: 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): try: pid = os.fork() if pid > 0: sys.exit(0) except OSError, e: write_log('ERROR', "fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1) os.setsid() os.chdir(loadconfig('DIRS', 'ROOT')) flwpath = loadconfig('DIRS', 'ROOT') sys.path.append(flwpath) os.umask(0) try: pid = os.fork() if pid > 0: sys.exit(0) except OSError, e: write_log('ERROR', "fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1)
def msyql_handler(): host = loadconfig('MYSQL','HOST') user = loadconfig('MYSQL','USER') passwd = loadconfig('MYSQL','PASSWD') or '' db = loadconfig('MYSQL','DB') port = int(loadconfig('MYSQL','PORT')) size = int(loadconfig('MYSQL','SIZE')) pooldb = PooledDB.PooledDB(MySQLdb, maxusage = size, host = host, user = user, passwd = passwd, db = db ,port = port,charset = 'utf8') return pooldb.connection()
def msyql_handler(): host = loadconfig('MYSQL', 'HOST') user = loadconfig('MYSQL', 'USER') passwd = loadconfig('MYSQL', 'PASSWD') or '' db = loadconfig('MYSQL', 'DB') port = int(loadconfig('MYSQL', 'PORT')) size = int(loadconfig('MYSQL', 'SIZE')) pooldb = PooledDB.PooledDB(MySQLdb, maxusage=size, host=host, user=user, passwd=passwd, db=db, port=port, charset='utf8') return pooldb.connection()
def watching_main(): while True: itemlists = get_watch_lists() itemqueue = watching_item_inQueue(itemlists) del itemlists maxprocess = int(loadconfig('MONITOR','maxprocess')) if multiprocessing.cpu_count() < maxprocess: maxprocess = multiprocessing.cpu_count() flwprocesspool = multiprocessing.Pool(processes = maxprocess) while True: if itemqueue.qsize() == 0: break itemlist = watching_item_outQueue(itemqueue) if itemlist == None: break funcname = watch_select(itemlist[3]) try: flwprocesspool.apply_async(funcname,(itemlist, )) except Exception as e: log_error('watching_main() process failed:'+str(e)) flwprocesspool.close() flwprocesspool.join() time.sleep(1)
def watching_main(): while True: itemlists = get_watch_lists() itemqueue = watching_item_inQueue(itemlists) del itemlists maxprocess = int(loadconfig('MONITOR', 'maxprocess')) if multiprocessing.cpu_count() < maxprocess: maxprocess = multiprocessing.cpu_count() flwprocesspool = multiprocessing.Pool(processes=maxprocess) while True: if itemqueue.qsize() == 0: break itemlist = watching_item_outQueue(itemqueue) if itemlist == None: break funcname = watch_select(itemlist[3]) try: flwprocesspool.apply_async(funcname, (itemlist, )) except Exception as e: log_error('watching_main() process failed:' + str(e)) flwprocesspool.close() flwprocesspool.join() time.sleep(1)
#!/usr/bin/python #encoding=utf-8 import sys,os,signal,simplejson,Queue,time from FlwLogLib import * from FlwAppLib import * from FlwMysqlLib import * from FlwWebsiteLib import website_alive_check from FlwPingLib import ping_alive_check,Ping from FlwUtil import checkip,loadconfig repeatnums = int(loadconfig('MONITORREPEAT','nums')) repeattimesleep = int(loadconfig('MONITORREPEAT','timesleep')) def get_timestamp(): return int(time.time()) def watching_item_inQueue(iteminfo): qitem = Queue.Queue() for info in iteminfo: qitem.put(info) return qitem def watching_item_outQueue(qitem): try: if qitem.qsize() != 0: itemlist = qitem.get(block = False) else: itemlist = None return itemlist
#!/usr/bin/python #encoding=utf-8 import sys, os, signal, simplejson, Queue, time from FlwLogLib import * from FlwAppLib import * from FlwMysqlLib import * from FlwWebsiteLib import website_alive_check from FlwPingLib import ping_alive_check, Ping from FlwUtil import checkip, loadconfig repeatnums = int(loadconfig('MONITORREPEAT', 'nums')) repeattimesleep = int(loadconfig('MONITORREPEAT', 'timesleep')) def get_timestamp(): return int(time.time()) def watching_item_inQueue(iteminfo): qitem = Queue.Queue() for info in iteminfo: qitem.put(info) return qitem def watching_item_outQueue(qitem): try: if qitem.qsize() != 0: itemlist = qitem.get(block=False) else:
break funcname = watch_select(itemlist[3]) try: flwprocesspool.apply_async(funcname,(itemlist, )) except Exception as e: log_error('watching_main() process failed:'+str(e)) flwprocesspool.close() flwprocesspool.join() time.sleep(1) class FlwMonitor(FlwDaemon): def _run(self): watching_main() if __name__ == '__main__': daemon = FlwMonitor(loadconfig('DIRS','ROOT') + 'FlwMonitor.pid') if len(sys.argv) == 2: if sys.argv[1].upper() == 'START': daemon.start() elif sys.argv[1].upper() == 'STOP': daemon.stop() elif sys.argv[1].upper() == 'RESTART': daemon.stop() daemon.start() else: print "Unknow Command!" print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(2) sys.exit(0) else: print "Usage: %s start|stop|restart" % sys.argv[0]
itemid = i[1] reason = i[5] level = i[4] sendnorice(itemid,level,reason) exec_sql("UPDATE fjk_monitor_alarm SET is_read=1,is_notice=1 WHERE id=%d" % noticeid) class FlwNotice(FlwDaemon): def _run(self): while True: noticerun() time.sleep(120) if __name__ == '__main__': daemon = FlwNotice(loadconfig('DIRS','ROOT') + 'FlwMsmEmailNotice.pid') if len(sys.argv) == 2: if 'START' == (sys.argv[1]).upper(): daemon.start() elif 'STOP' == (sys.argv[1]).upper(): daemon.stop() elif 'RESTART' == (sys.argv[1]).upper(): daemon.restart() else: print "Unknow Command!" print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(2) sys.exit(0) else: print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(0)
try: flwprocesspool.apply_async(funcname, (itemlist, )) except Exception as e: log_error('watching_main() process failed:' + str(e)) flwprocesspool.close() flwprocesspool.join() time.sleep(1) class FlwMonitor(FlwDaemon): def _run(self): watching_main() if __name__ == '__main__': daemon = FlwMonitor(loadconfig('DIRS', 'ROOT') + 'FlwMonitor.pid') if len(sys.argv) == 2: if sys.argv[1].upper() == 'START': daemon.start() elif sys.argv[1].upper() == 'STOP': daemon.stop() elif sys.argv[1].upper() == 'RESTART': daemon.stop() daemon.start() else: print "Unknow Command!" print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(2) sys.exit(0) else: print "Usage: %s start|stop|restart" % sys.argv[0]
sendnorice(itemid, level, reason) exec_sql( "UPDATE fjk_monitor_alarm SET is_read=1,is_notice=1 WHERE id=%d" % noticeid) class FlwNotice(FlwDaemon): def _run(self): while True: noticerun() time.sleep(120) if __name__ == '__main__': daemon = FlwNotice(loadconfig('DIRS', 'ROOT') + 'FlwMsmEmailNotice.pid') if len(sys.argv) == 2: if 'START' == (sys.argv[1]).upper(): daemon.start() elif 'STOP' == (sys.argv[1]).upper(): daemon.stop() elif 'RESTART' == (sys.argv[1]).upper(): daemon.restart() else: print "Unknow Command!" print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(2) sys.exit(0) else: print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(0)
def mysql_phandler(): MDB = MySQLdb.connect(host = loadconfig('MYSQL','HOST'),user = loadconfig('MYSQL','USER'), passwd = loadconfig('MYSQL','PASSWD'),db = loadconfig('MYSQL','DB'),port = int(loadconfig('MYSQL','PORT')),charset = 'utf8') return MDB