def main(): """main() - main function Args: None. Returns: Nothing. """ parse_args() # TODO load previous state from disk for _ in range(LOG.size): LOG.add((None, [])) # Set up HTTP API wwwroot = Resource() wwwroot.putChild(b"", PingerAPIHelp()) wwwroot.putChild(b"elapsed", PingerAPIElapsed()) wwwroot.putChild(b"check", PingerAPICheck()) wwwroot.putChild(b"up", PingerAPIUp()) wwwroot.putChild(b"down", PingerAPIDown()) wwwroot.putChild(b"stats", PingerAPIStats()) reactor.listenTCP(Settings.get("port"), server.Site(wwwroot)) # START THE REACTOR!@#$ reactor.callInThread(check_hosts, Settings.get("hosts")) reactor.run()
def connect(): if not MysqlServer.cnf: settings = Settings.get() if settings['db_use'] == 'master': MysqlServer.cnf = settings['mysqlserver_master'] elif settings['db_use'] == 'slave': MysqlServer.cnf = settings['mysqlserver_slave'] return torndb.Connection( host='%s:%s' % (MysqlServer.cnf['host'], MysqlServer.cnf['port']), database=MysqlServer.cnf['db'], user=MysqlServer.cnf['user'], password=MysqlServer.cnf['pwd'])
def connect(): if not MysqlServer.cnf: settings = Settings.get() if settings['db_use'] == 'master': MysqlServer.cnf = settings['mysqlserver_master'] elif settings['db_use'] == 'slave': MysqlServer.cnf = settings['mysqlserver_slave'] return MySQLdb.connect(host=MysqlServer.cnf['host'], port=int(MysqlServer.cnf['port']), db=MysqlServer.cnf['db'], user=MysqlServer.cnf['user'], passwd=MysqlServer.cnf['pwd'], cursorclass=cursors.DictCursor, charset='utf8')
def check_hosts(ips): """check_hosts() - ping list of hosts, log them, and do things to said hosts Args: ips (list) - list of ips to ping processes (int) - number of ping processes to execute at a time. Returns: Nothing, however this calls itself at the end in order to achieve a continuous scan with as little effort from my end as possible. """ processes = Settings.get("processes") start_time = time() pool = Pool(processes) output = pool.map(ping, ips) pool.close() pool.join() finish_time = time() - start_time LOG.add((finish_time, output)) current_state = LOG.log[-1] previous_state = LOG.log[-2] current_hosts = current_state[1] previous_hosts = previous_state[1] if previous_hosts == []: for current in current_hosts: if current[1]: print("up", current) for current in current_hosts: for previous in previous_hosts: if current[0] == previous[0]: # hosts match current_up = True if current[1] else False previous_up = True if previous[1] else False if current_up != previous_up: # state changed! # TODO "traps" print("up" if current_up else "down", current) # lolloop reactor.callInThread(check_hosts, ips)
import argparse from time import time from subprocess import call from multiprocessing import Pool from textwrap import dedent from twisted.web import server, resource from twisted.web.resource import Resource from twisted.internet import reactor from rollinglog.rollinglog import RollingLog from settings.settings import Settings # Lol globals. LOG = RollingLog(Settings.get("logsize")) def hostname_to_ip(hostname): """hostname_to_ip() - Resolve a hostname. Args: hostname (str) - Hostname to resolve. Returns: String containing IP address in dotted quad notation on success. None if hostname could not be resolved. """ try: resolved = socket.getaddrinfo(hostname, 0, 0, socket.SOCK_STREAM) except socket.gaierror:
class Fennec(object): def __init__(self, files_list, root_path=None): if (root_path == None): root_path = path.abspath( path.join( path.abspath( path.dirname( path.abspath( inspect.getfile(inspect.currentframe())))), "..")) self.logger = Logger(root_path) self.settings = Settings(root_path) self.context = Context(self.settings) for filepath in files_list: self.context.load_path(filepath) def set_settings(self, name, value): self.settings.set(name, value) def set_configfile(self, path_config): # try importing from given path try: self.settings.read_file(path_config) return except IOError: pass # try importing in default config dir root_path = self.settings.get('root_path') try: self.settings.read_file( path.join(root_path, "settings", "config", path_config + '.py')) return except IOError: pass # try importing from current working directory in config dir curr_dir = path.abspath(os.getcwd()) try: self.settings.read_file( path.join(curr_dir, "config", path_config + '.py')) return except: self.logger.get_logger().error( "Could not impot configuration file") raise def add_paths(self, paths): for path in paths: self.context.load_path(path) def audit(self): """ Launch code audit process """ self.context.audit() def clean(self): """ Cleans the logs, traces and temporary files (if there are some) """ log_path = os.path.join(self.settings.get('root_path'), "log", "*") traces_path = os.path.join(self.settings.get('root_path'), "trace", "*") to_remove = glob.glob(log_path) for node in to_remove: if node.endswith('.log'): os.remove(node) to_remove = glob.glob(traces_path) for node in to_remove: if node.endswith('.log'): os.remove(node) self.logger.get_logger().info("Finished cleaning.")
def __create_pool(): cnf = Settings.get()['redisserver'] RedisServer._connect_pool = redis.ConnectionPool(host=cnf['host'], port=cnf['port'], db=cnf['db'], password=cnf['pwd'])
def __create_pool(): cnf = Settings.get()['rediscluster'] RedisClusterServer._cluster_connect_pool = redis.ConnectionPool(host=cnf['host'], port=cnf['port'], password=cnf['pwd'])
from settings.settings import Settings from tools.common import warning_title_clickbooking, warning_text_clickbooking, warning_title_clickbooking_stic, \ warning_text_clickbooking_stic from tools.redisserver import RedisServer from tools.logger import Logger from tools.warning import WarningMsg if __name__ == '__main__': try: if len(sys.argv) - 1 > 0: app_startup_path = os.path.dirname(os.path.realpath(__file__)) params = sys.argv[1:] assert 'environment' in params[ 0], "Must be contains the name 'environment' at first argv for launching..." environment = params[0].split('=')[1].strip(' ') settings = Settings.get(app_startup_path, environment) settings['redisserver']['db'] = '' for p in params[1:]: name = p.split('=')[0].strip(' ') if name == 'log_path': settings['log_path'] = p.split('=')[1].strip(' ') elif name == 'js_url': settings['js_url'] = p.split('=')[1].strip(' ') else: print 'Please give some args like:environment=dev log_path=/source/python/... for launching application! Well be exit 0!' sys.exit(0) except Exception as e: print 'Application init settings error!!! Well be exit -1!\n%s' % traceback.format_exc( ) sys.exit(-1)
class Fennec(object): def __init__(self, files_list, root_path = None): if (root_path == None): root_path = path.abspath(path.join( path.abspath(path.dirname( path.abspath(inspect.getfile( inspect.currentframe())))), "..")) self.logger = Logger(root_path) self.settings = Settings(root_path) self.context = Context(self.settings) for filepath in files_list: self.context.load_path(filepath) def set_settings(self, name, value): self.settings.set(name, value) def set_configfile(self, path_config): # try importing from given path try: self.settings.read_file(path_config) return except IOError: pass # try importing in default config dir root_path = self.settings.get('root_path') try: self.settings.read_file(path.join( root_path, "settings", "config", path_config + '.py')) return except IOError: pass # try importing from current working directory in config dir curr_dir = path.abspath(os.getcwd()) try: self.settings.read_file(path.join( curr_dir, "config", path_config + '.py')) return except: self.logger.get_logger().error("Could not impot configuration file") raise def add_paths(self, paths): for path in paths: self.context.load_path(path) def audit(self): """ Launch code audit process """ self.context.audit() def clean(self): """ Cleans the logs, traces and temporary files (if there are some) """ log_path = os.path.join(self.settings.get('root_path'), "log", "*") traces_path = os.path.join(self.settings.get('root_path'), "trace", "*") to_remove = glob.glob(log_path) for node in to_remove: if node.endswith('.log'): os.remove(node) to_remove = glob.glob(traces_path) for node in to_remove: if node.endswith('.log'): os.remove(node) self.logger.get_logger().info("Finished cleaning.")