def print_line_summary(self): ''' Print a one-line summary.''' ck = 0 ok = 0 nok = 0 critical = 0 error = 0 warning = 0 notice = 0 for c in self.checks: ck += 1 if c.severity == cmt.SEVERITY_CRITICAL: critical += 1 elif c.severity == cmt.SEVERITY_ERROR: error += 1 elif c.severity == cmt.SEVERITY_WARNING: warning += 1 elif c.severity == cmt.SEVERITY_NOTICE: notice += 1 else: ok += 1 nok = ck - ok if ck == 0: percent = 100 else: percent = int(100 * ok / ck) print() logit( "SEVERITY={} - {}/{} OK ({} %) - {} NOK : {} criticial - {} error - {} warning - {} notice." .format(self.get_severity_string(), ok, ck, percent, nok, critical, error, warning, notice)) print()
def __init__(self, module="nomodule", check="noname", conf={}, opt={}): self.group = cmt.CONF['global'].get('cmt_group', 'nogroup') self.node = cmt.CONF['global'].get('cmt_node', 'nonode') self.node_env = cmt.CONF['global'].get('cmt_node_env', 'noenv') self.node_role = cmt.CONF['global'].get('cmt_node_role', 'norole') self.node_location = cmt.CONF['global'].get('cmt_node_location', 'nolocation') self.module = module self.check = check self.opt = opt # opt given at init time by perform_check external creator self.result = "ok" # ok, nok, skip self.result_info = "" # human info about check run self.version = cmt.VERSION_NUMBER # new 1.6.1 self.message = [] # new 2.0 - rawdata / multi-events (sendfile, mysqldata) self.multievent = [] # list of individual points of data self.checkitems = [] # fields from conf self.conf = conf # severity : 1=critical, 2=error, 3=warning, 4=notice, 5=none # set by modules ; shifted by severity_max configuration self.severity = cmt.SEVERITY_NONE # DEFAULT = nothing wrong # severity_max ; default = critical self.severity_max = cmt.SEVERITY_CRITICAL a = conf.get('severity_max', 'critical') if a == 'critical': self.severity_max = cmt.SEVERITY_CRITICAL elif a == 'error': self.severity_max = cmt.SEVERITY_ERROR elif a == 'warning': self.severity_max = cmt.SEVERITY_WARNING elif a == 'notice': self.severity_max = cmt.SEVERITY_NOTICE elif a == 'none': self.severity_max = cmt.SEVERITY_NONE else: logit("Unknown severity_max {} in ({},{}) - default to critical.". format(a, module, check)) # events/transition : NEW, ACTIVE, DOWN, NONE - computed (hysteresis, delay) self.alert = 0 # set by framework after Check is performed, if pager_enable and alert ! NONE self.pager = False # persist values from previous run for same get_id() id = self.get_id() self.persist = cmt.PERSIST.get_key(id, {})
def upload(self): header = { 'Host':'www.freeimagehosting.net', 'Origin':'http://www.freeimagehosting.net', 'Referer':'http://www.freeimagehosting.net/upload.php', } kw = (('attached',[self.img])) jmp = self.req.post_multipart(URL['upload'],header,kw) link = PARSE_SHARE_LINK.findall(jmp.read())[0] logger.logit('post %s to %s'%(self.img,link)) #log return link
def send_test(): ''' send TEST alert to all pagers. ''' for pager in cmt.CONF['pagers']: if cmt.ARGS["pager"]: if pager not in cmt.ARGS["pager"][0]: continue pagerconf = cmt.CONF['pagers'][pager] pagertype = pagerconf.get('type', 'unknown') if pagertype == "team_channel" or pagertype == "teams": message, content = teams_build_test_message() r = teams_send_message(message=message, pagerconf=pagerconf) if r: logit("Pager Teams <{}> : test OK".format(pager)) else: logit("Pager Teams <{}> : test FAILED".format(pager)) elif pagertype == "pagerduty": message, content = pagerduty_build_test_message( pagerconf=pagerconf) r = pagerduty_send_message(message=message, pagerconf=pagerconf) if r: logit("Pager PagerDuty {} : test OK".format(pager)) else: logit("Pager PagerDuty {} : test FAILED".format(pager)) elif pagertype == "smtp": pass else: debug("Unknown pager type in conf for ", pager)
def print_header(self): if cmt.ARGS['cron']: print("-" * 60) logit("Starting ...") print() else: print('-' * 60) print(cmt.VERSION) print('-' * 60) print("cmt_group : ", cmt.CONF['global']['cmt_group']) print("cmt_node : ", cmt.CONF['global']['cmt_node']) print("config file : ", cmt.CONF['__config_file']) print()
def _set_wallpaper(self): try: client = gconf.client_get_default() except: raise errors.Err(errors.ECGCFCREAT, errors.ENGCFCREAT, errors.MSGCFCREAT) else: if not client.set_string("/desktop/gnome/background/picture_filename", self.xml): logger.logit(errors.MSGCFSETWP) raise errors.WallGconfError(errors.ECGCFSETWP, errors.ENGCFSETWP, errors.MSGCFSETWP) else: if not client.set_string("/desktop/gnome/background/picture_options", self.style): logger.logit(errors.MSGCFWPSTY) raise errors.WallGconfError(errors.ECGCFWPSTY, errors.ENGCFWPSTY, errors.MSGCFWPSTY) return True return False
def post_text(self, links, box_name=None ,title='gwaller', desc='', tags='gwaller,wallpaper', opt_syncs='0', header={}): if box_name is None: box_name = self._get_boxes_name().keys()[0] out = '' for link in links[:-1]: out += '<p><a href="%s">%s</a></p>'%(link,link) out += '<p><a href="%s">%s</a><br /></p>'%(links[-1],links[-1]) print out postdata = { 'formKey':self.formKey, 'title':title, 'content':out, 'desc':'<p>' + desc + '<br /></p>', 'tags':tags, 'creativeCommonsType':'by_nc_nd', 'privacy':0 } logger.logit('Post %s to %s'%(';'.join(links),box_name)) return self._post_factory('text',box_name, postdata, opt_syncs,header)
def load_conf_master(): ''' load first config file aka root/master config file ''' debug("Load master conf") if cmt.ARGS['conf']: config_file = cmt.ARGS['conf'] else: config_file = os.path.join(cmt.HOME_DIR, "conf.yml") debug("Config file : ", config_file) if not os.path.exists(config_file): logit('Config file not found : ' + config_file) sys.exit() # load master conf conf = conf_load_file(config_file) conf["__config_file"] = config_file conf_add_top_entries(conf) return conf
def conf_load_http(url): headers = {'Content-Type': 'application/text'} if cmt.ARGS['devmode']: print("DEVMODE : GET ", url, headers) # real try: r = requests.get(url, headers=headers, verify=False, allow_redirects=False, timeout=cmt.REMOTE_CONFIG_TIMEOUT) except Exception as e: logit("Load remote conf failed (network) : {}".format(e)) return None if r.status_code != 200: debug("Load remote conf failed : {}".format(+r.status_code)) return None debug("Load remote conf OK : ", r.text) return r.text
def graylog_send_udp_gelf(metroconf={}, data=""): # data = '"demo":"42"' # mess = '{ "version":"1.1", "host":"host-test", "short_message":"CMT gelf test", ' + data + ' }' host = metroconf.get('host','localhost') port = metroconf.get('port', 12201) if cmt.ARGS['devmode']: print("DEVMODE : graylog udp : ", host, port, data) return binpayload = bytes(str(data), "utf-8") sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_address = (host, port) try: sock.sendto(binpayload, server_address) sock.close() debug("Message sent to graylog(udp/gelf)") except Exception as e: logit("Error - couldn't send graylod message (udp/gelf) to {} - {}".format(host, e))
def post_image(self, image, box_name=None, link=None,opt_syncs=None, header={}): if box_name is None: box_name = self._get_boxes_name().keys()[0] #if no box_name given, use the first as default if link is None: link = 'http://%s.diandian.com' % box_name if opt_syncs is None: opt_syncs='0' jsn = self._json_dict(self._upload_image(box_name, image, opt_syncs, header)) _id, _url = jsn['id'], jsn['url'] desc = '<img src="%s" id="%s" />'%(_url, _id) out = self._json_dict(self.post_link(box_name, link, desc=desc)) #may need verification code suc = out.get("publisherAfterTipsPortlet",None) logger.logit('post image %s to %s'%(image, box_name)) #log if suc is not None: return out else: #get the verification code, before that generate the link of verification code #show it on qt dialog (UI) #use local program to open it #input it and post again pass
def service_status(self): if self.ostype == "W": try: output = subprocess.check_output( 'sc query "MongoDB" | find "STATE"', shell=True) #retval=subprocess.call(['runas', '/user:Administrator', 'net start MongoDB']) status = output.split() if status[3] == "RUNNING": print "MongoDB service is Running" elif status[3] == "STOPPED": print "MongoDB service is Stopped" logger.logit(self.scriptname, 'd', 'mongodb process stopped', 'E', self.lineno()) else: print "MongoDB Service is in Unkown state" except subprocess.CalledProcessError as err: print "error code", err.returncode, err.output logger.logit(self.scriptname, 'd', 'service status unknown error', 'E', self.lineno()) elif self.ostype == "L": try: output = subprocess.check_output( 'sc query "MongoDB" | find "STATE"', shell=True) #retval=subprocess.call(['runas', '/user:Administrator', 'net start MongoDB']) status = output.split() if status[3] == "RUNNING": print "MongoDB service is Running" elif status[3] == "STOPPED": print "MongoDB service is Stopped" logger.logit(self.scriptname, 'd', 'mongodb process stopped', 'E', self.lineno()) else: print "MongoDB Service is in Unkown state" except subprocess.CalledProcessError as err: print "error code", err.returncode, err.output logger.logit(self.scriptname, 'd', 'service status unknown error', 'E', self.lineno())
def elastic_send_http_json(metroconf={}, data=""): headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} url = metroconf.get('url','http://localhost/') ssl_verify = metroconf.get("ssl_verify", True) is True http_code = metroconf.get("http_code", 201) proxies, proxy_noenv = get_proxies(metroconf) if proxy_noenv: cmt.SESSION.trust_env = False proxies={} if cmt.METROLOGY_HTTP_SUSPENDED: logit("suspended - HTTP graylog message (http/gelf) not sent to " + str(url)) return if cmt.ARGS['devmode']: print("DEVMODE : elastic http : ", url, data, headers) return try: r = cmt.SESSION.post( url, data=data, headers=headers, verify=ssl_verify, proxies=proxies, timeout=cmt.METROLOGY_HTTP_TIMEOUT) debug("Message sent to elastic(http/json) ; status = " + str(r.status_code)) except Exception as e: logit("Error - couldn't send elastic message (http/json) to {} - {}".format(url, e)) cmt.METROLOGY_HTTP_SUSPENDED = True return if http_code>0: if r.status_code != http_code: logit("Alert couldn't send to Elastic - bad response: " + str(r)) return return
mypause = conf.get_startoffset() time.sleep(mypause) # Persist cmt.PERSIST = persist.Persist(file=cmt.DEFAULT_PERSIST_FILE) if cmt.ARGS["nopersist"]: cmt.PERSIST.dict = {} lastrun = cmt.PERSIST.get_key("cmt_last_run", 0) # remote conf (url) or cached conf conf.load_conf_remote(cmt.CONF) # check master switch / CMT disabled ? ts_global_enable = cmt.CONF['global'].get('enable', 'no') if not conf.is_timeswitch_on(ts_global_enable): logit("CMT globally disabled by conf") sys.exit() # CLI : check config option ? if cmt.ARGS["checkconfig"]: print(json.dumps(cmt.CONF, indent=2)) print("config OK.") sys.exit() # CLI : Send test message to Pagers ? if cmt.ARGS["pager_test"]: pager.send_test() sys.exit() # CLI : List available modules option ? if cmt.ARGS["listmodules"]:
def influxdb_send_http(metroconf={}, data=""): headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': '*/*'} url = metroconf.get('url', 'http://localhost/') username = metroconf.get('username','') password = metroconf.get('password','') token = metroconf.get('token','') ssl_verify = metroconf.get("ssl_verify", True) is True http_code = metroconf.get("http_code", 204) proxies, proxy_noenv = get_proxies(metroconf) if proxy_noenv: cmt.SESSION.trust_env = False proxies={} if cmt.METROLOGY_INFLUXDB_SUSPENDED: logit("suspended - INFLUXDB message suspended/not sent to " + str(url)) return if cmt.ARGS['devmode']: print("DEVMODE : INFLUXDB : ", url) print(data) return try: # token authentication (V1/V2) # --header "Authorization: Token YOURAUTHTOKEN" \ if len(token) > 0: headers["Authorization"] = "Token {}".format(token) r = cmt.SESSION.post(url, data=data, headers=headers, verify=ssl_verify, proxies=proxies, timeout=cmt.METROLOGY_INFLUXDB_TIMEOUT) # basic authentication elif len(username)>0: r = cmt.SESSION.post(url, data=data, headers=headers, verify=ssl_verify, proxies=proxies, auth=(username, password), timeout=cmt.METROLOGY_INFLUXDB_TIMEOUT) # no auth / auth in URL ?u=XXX&p=XXX else: r = cmt.SESSION.post(url, data=data, headers=headers, verify=ssl_verify, proxies=proxies, timeout=cmt.METROLOGY_INFLUXDB_TIMEOUT) debug("Message sent to INFLUXDB ; status = " + str(r.status_code)) except Exception as e: logit("Error - couldn't send INFLUXDB message to {} - {}".format(url, e)) cmt.METROLOGY_INFLUXDB_SUSPENDED = True return if http_code>0: if r.status_code != http_code: logit("Alert couldn't send to InfluxDB - bad response: " + str(r)) return return
def send_real(report): ''' Send REAL alerts to all Pagers ''' # global : Pager enabled (global section, master switch) ? tmp = cmt.CONF['global'].get("enable_pager", "no") if not conf.is_timeswitch_on(tmp): debug("Pager : disabled/inactive in global config.") return # check if alert exists in report (report.pager set to True) if not report.pager: debug("Pager : no alerts in report / no pager to be fired") return count_pager_fired = 0 persist_last_timestamps = cmt.PERSIST.get_key("pager_last_timestamps", {}) for pager in cmt.CONF['pagers']: if cmt.ARGS["pager"]: if pager not in cmt.ARGS["pager"][0]: continue debug("Processing pager {}".format(pager)) pagerconf = cmt.CONF['pagers'][pager] pagertype = pagerconf.get( 'type', 'unknown') # team_channel/teams, pagerduty, smtp pager_rate_limit = int(pagerconf.get("rate_limit", 7200)) timerange = pagerconf.get("enable", "yes") pagermode = pagerconf.get('mode', 'managed') # is this pager active ? if not conf.is_timeswitch_on(timerange): debug("Pager disabled/inactive in conf : ", pager) continue # which mode ? managed[default], allnotifications selected_alerts = [cmt.ALERT_NEW] if pagermode == "allnotifications": selected_alerts = [cmt.ALERT_NEW, cmt.ALERT_ACTIVE, cmt.ALERT_DOWN] #print(pagerconf, pagermode, selected_alerts) # per pager rate_limit : if managed, handle rate_limit if not cmt.ARGS['no_pager_rate_limit'] and not cmt.ARGS['devmode']: if pagermode == "managed": t1 = persist_last_timestamps.get(pager, 0) t2 = int(time.time()) if t2 - t1 <= pager_rate_limit: waitfor = t1 + pager_rate_limit - t2 logit( "Pager rate-limit reached ({} sec to wait) for pager {}" .format(waitfor, pager)) continue else: persist_last_timestamps[pager] = t2 else: # not managed : no rate-limit pass # build and send messages # ------------------------ # teams if pagertype == "team_channel" or pagertype == "teams": message, content = teams_build_message( report, selected_alerts=selected_alerts) if len(content) > 0: r = teams_send_message(message=message, pagerconf=pagerconf) if r: logit("Pager Teams {} : OK".format(pager)) count_pager_fired += 1 else: logit("Pager Teams {} : FAILED".format(pager)) else: # nothing to send pass # pagerduty elif pagertype == "pagerduty": message, content = pagerduty_build_message( report, pagerconf=pagerconf, selected_alerts=selected_alerts) if len(content) > 0: r = pagerduty_send_message(message=message, pagerconf=pagerconf) if r: logit("Pager PagerDuty {} : OK".format(pager)) count_pager_fired += 1 else: logit("Pager PagerDuty {} : FAILED".format(pager)) else: # nothing to send pass # smtp elif pagertype == "smtp": #smtp_sendreal(pager, pagerconf, report) pass #unknown else: debug("Unknown pager type in conf for ", pager) if count_pager_fired > 0: logit("Pager fired : {}".format(count_pager_fired)) else: logit("No pager fired.") # update rate_limit if not cmt.ARGS['devmode']: cmt.PERSIST.set_key("pager_last_timestamps", persist_last_timestamps)
# -*- coding: utf-8 -*- import features import mlRepo import logger import sourceData #Acquire the data from sklearn.model_selection import train_test_split df, columns, results = sourceData.getData() algos = mlRepo.getAlgos(None) result=df["result1"] for feat in features.getFeatureSets(df, columns, results): X_train, X_test, Y_train, Y_test = train_test_split(feat, result, test_size=0.4, random_state=101) for key in algos: accuracy = algos[key].execute(X_train, Y_train, X_test, Y_test) logger.logit (algos[key].getName(), accuracy, feat.columns) #Store the values against the featurs and algos for comparing it later
def perform_check(checkname, modulename): debug2("Starting check : ", checkname) # Is module in GLOBAL MAP ? if modulename not in cmt.GLOBAL_MODULE_MAP: logit("Unknown module in configuration: ", modulename) return "continue" # get configuration for this check checkconf = cmt.CONF[modulename][checkname] # check enabled ? ts_check = checkconf.get('enable', 'yes') if not conf.is_timeswitch_on(ts_check): debug("check disabled by conf ", checkname) return "continue" # check if module is filtered in ARGS if not args.is_module_allowed_in_args(modulename): #check_result.result = "skip" #check_result.result_info = "module not requested (args)" return "continue" # prepare options sent to Module code my_opt = {} # some checks are exclusive / standalone # Check if it is a standalone check (one module, one check) if args.is_module_alone_in_args(modulename): my_opt["single_module_run"] = True else: my_opt["single_module_run"] = False # particular checkname requested ? (--check option) # NB : several modules can match for the same chechname which is not a PK accross full config my_opt["specific_checkname_run"] = False if cmt.ARGS["check"]: if cmt.ARGS["check"] == checkname: debug2(" specific check name : match %s" % checkname) my_opt["specific_checkname_run"] = True else: debug2(" specific check name : skip %s" % checkname) return "continue" # create check object check_result = Check(module=modulename, check=checkname, conf=checkconf, opt=my_opt) # Add tags/kv check_result.add_tags() # print header to CLI if cmt.ARGS['cron'] or cmt.ARGS['short']: pass else: check_result.print_to_cli_detail_head() # check if root privilege is required conf_rootreq = checkconf.get('root_required', False) is True if conf_rootreq: if (os.getuid() != 0): debug("check %s must run as root." % checkname) check_result.result = "skip" check_result.result_info = "must run as root" check_result.print_to_cli_skipped() return "continue" # verify frequency in cron mode if cmt.ARGS['cron']: if not check_result.frequency(): check_result.result = "skip" check_result.result_info = "check skipped (frequency)" check_result.print_to_cli_skipped() return "continue" # HERE / Future : give check_result the needed Module Conf, Global Conf ... # TODO : if --available, call different function # ********************************************************* # **** ACTUAL CHECK IS DONE HERE **** # ********************************************************* check_result = cmt.GLOBAL_MODULE_MAP[modulename]['check'](check_result) # --------------- # process results # --------------- # option = available => not a real run ; just display discovered target and quit if cmt.ARGS["available"]: return "break" # if check skipped by module itself if check_result.result == "skip": check_result.resul_info = check_result.message check_result.print_to_cli_skipped() debug(" skipped in module") return "continue" # adjust severity to severity_max for this check check_result.adjust_severity() # compute alert transition : NONE, NEW, ACTIVE, DOWN ; hysteresis check_result.compute_alert() # If pager enabled (at check level), and alert exists : set pager True check_result.compute_pager() # Print to CLI if cmt.ARGS['cron'] or cmt.ARGS['short']: check_result.print_to_cli_short() else: check_result.print_to_cli_detail() # keep returned Persist structure in check_result cmt.PERSIST.set_key(check_result.get_id(), check_result.persist) # Metrology if cmt.ARGS['cron'] or cmt.ARGS["report"]: # check_result.send_metrology() metrology.send_metrology(check_result) # add Check to report return check_result
# Solutions/Syntax/readit.py # import logger line = input("enter something") logger.logit( line) print( type( line)) print( dir( line))