def accept_score(self, tx_hash): if not self._owner: die('Error: owner should be specified to invoke acceptScore') params = { "txHash": tx_hash } return self._invoke(self._owner, "acceptScore", params)
def main(): parser = argparse.ArgumentParser() # parameter for the TLV. parser.add_argument( 'TLV', help='TLV string (coded in hexadecimal).' ) # doctest flag. parser.add_argument( '-t', '--test', help='run the doctests and exit.', action='store_true' ) args = parser.parse_args() if args.test: doctest.testmod() return if not validate(args.TLV): util.die("A valid TLV value must be encoded in HEX and be an integer"\ "multiple of Bytes greater then 0") print human(args.TLV) return
def generate(self, _): method, x = self.parse_method(), None if method == "align": name, x = "DOT_ALIGN", self.parse_obj[1].eval() elif method == "ascii": name, x = "DOT_ASCII", self.parse_obj[1] elif method == "text": name, x = "DOT_TEXT", self.parse_obj[1] elif method == "breakpoint": name = "DOT_BREAKPOINT" elif method == "protect": name = "DOT_PROTECT" elif method == "unprotect": name = "DOT_UNPROTECT" elif method == "options": # TODO Do a better job of cleaning up the whitespace between these options. name, x = "OPTIONS", self.parse_obj[1].split(" ")[1:] elif method == "label": name, x = "LABEL", self.parse_obj[0].eval().name() # print name, x # if x == "start": # import pdb;pdb.set_trace() elif method == "include": return [] else: util.die("Unknown processor directive found: " + self.parse_method()) return [AssemblerInst(name, x)]
def fetch_roster(self): try: xmpp.ClientXMPP.get_roster(self) logging.info("Roster: %s" % self.roster) return self.roster except Exception, exc: util.die("Couldn't fetch roster: %s" % exc)
def pushover(data, context): ''' Sends alert to Pushovers API. data['token']: Application token data['user']: User token data['priority']: At what priority to send notification. https://pushover.net/api#priority context['title']: used to craft notification message context['message']: used to craft notification message pushmessage: Prettier updown message for notification ''' if 'token' not in data or 'user' not in data or 'priority' not in data: util.die('alert_pushover: details missing!') return if context['updown'] == 'down': pushmessage = context['title'] + " - " + context['message'] elif context['updown'] == 'up': pushmessage = context['title'] import httplib, urllib conn = httplib.HTTPSConnection("api.pushover.net:443") conn.request("POST", "/1/messages.json", urllib.urlencode({ "token": data['token'], "user": data['user'], "priority": data['priority'], "message": pushmessage, }), { "Content-type": "application/x-www-form-urlencoded" })
def handle_change(self, thread_name, check_id, check_name, lock_uid, status, check_result): if status == 'offline': updown = 'down' elif status == 'online': updown = 'up' safe_print("[%s] ... confirmed, target is %s state now", (thread_name, status)) update_result = Check.objects.raw("UPDATE checks SET status = %s, confirmations = 0, `lock` = '', last_checked = NOW() WHERE id = %s AND `lock` = %s", (status, check_id, lock_uid)) if update_result.rowcount == 1: # we still had the lock at the point where status was toggled # then, send the alert alert_result = database.query("SELECT contacts.id, contacts.type, contacts.data FROM contacts, alerts WHERE contacts.id = alerts.contact_id AND alerts.check_id = %s AND alerts.type IN ('both', %s)", (check_id, updown)) for alert_row in alert_result.fetchall(): safe_print("[%s] ... alerting contact %d", (thread_name, alert_row['id'])) alert_func = getattr(alerts, alert_row['type'], None) if not alert_func: util.die("Invalid alert handler [%s]!" % (alert_row['type'])) # build context context = {} context['check_id'] = check_id context['check_name'] = check_name context['contact_id'] = alert_row['id'] context['title'] = "Check %s: %s" % (status, check_name) context['status'] = status context['updown'] = updown context['message'] = check_result['message'] alert_func(util.decode(alert_row['data']), context) # also add an event database.query("INSERT INTO check_events (check_id, type) VALUES (%s, %s)", (check_id, updown))
def sms_twilio(data, context): ''' Sends an SMS message via Twilio to the given number. The message is "[title] message" config must include the strings twilio_accountsid, twilio_authtoken, and twilio_number data['number']: phone number to send SMS message to. data['twilio_accountsid'], data['twilio_authtoken'], data['twilio_number']: optional Twilio configuration context['title']: used in creating SMS message context['message']: used in creating SMS message ''' from config import config from twilio.rest import TwilioRestClient if 'number' not in data: util.die('alert_sms_twilio: missing number') config_target = config if 'twilio_accountsid' in data and 'twilio_authtoken' in data and 'twilio_number' in data: config_target = data sms_message = "[%s] %s" % (context['title'], context['message']) client = TwilioRestClient(config_target['twilio_accountsid'], config_target['twilio_authtoken']) message = client.messages.create(body=sms_message, to=data['number'], from_=config_target['twilio_number'])
def _get_new_delegations(self, result): delegations = self.convert_to_map(result['delegations']) voting_power = int(result['votingPower'], 16) self.print_delegations(delegations, voting_power) while True: try: confirm = input( '\n==> The address you want to set (\'q\' for finish): ') if confirm == 'q': return delegations address = address_type(confirm) self.print_prep_info(address) maximum = voting_power + int(delegations.get(address, '0x0'), 16) amount = self._check_value( input(f'Delegation amount (max: {maximum}): '), maximum) delegations[address] = hex(amount) voting_power = maximum - amount self.print_delegations(delegations, voting_power) except KeyboardInterrupt: die('exit') except argparse.ArgumentTypeError: print('Error: invalid address') continue except ValueError as e: print(e.__str__()) continue except JSONRPCException: continue
def __init__(self, logfile, prefs, lock_file, ignore_offset=0, first_time=0, noemail=0, daemon=0): self.__denied_hosts = {} self.__prefs = prefs self.__lock_file = lock_file self.__first_time = first_time self.__noemail = noemail self.__report = Report(prefs.get("HOSTNAME_LOOKUP"), is_true(prefs['SYSLOG_REPORT'])) self.__daemon = daemon self.__sync_server = prefs.get('SYNC_SERVER') self.__sync_upload = is_true(prefs.get("SYNC_UPLOAD")) self.__sync_download = is_true(prefs.get("SYNC_DOWNLOAD")) r = Restricted(prefs) self.__restricted = r.get_restricted() info("restricted: %s", self.__restricted) self.init_regex() try: self.file_tracker = FileTracker(self.__prefs.get('WORK_DIR'), logfile) except Exception, e: self.__lock_file.remove() die("Can't read: %s" % logfile, e)
def _generate_code_for_emit_action(self, bind_or_emit): """Emit code for emit action """ if bind_or_emit == 'BIND': if self.emit_type == 'letters' or self.field_name == None: return '' elif self.emit_type == 'numeric': op_accessor = encutil.enc_strings['op_accessor'] operand_setter = "%s_set_%s" % (op_accessor, self.field_name.lower()) obj_name = encutil.enc_strings['obj_str'] hex_val = hex(self.int_value) code = "%s(%s, %s);" % (operand_setter, obj_name, hex_val) return [' ' + code] else: util.die("Unknown emit_type %s" % self.emit_type) else: # EMIT emit_util_function = encutil.enc_strings['emit_util_function'] obj_name = encutil.enc_strings['obj_str'] nbits = self.nbits code = '' if self.field_name == None: if self.emit_type == 'numeric': hex_val = hex(self.int_value) code = "%s(%s, %d, %s);" % (emit_util_function, obj_name, nbits, hex_val) else: util.die("must have field name for letter action") else: op_accessor = encutil.enc_strings['op_accessor'] operand_getter = "%s_get_%s(%s)" % ( op_accessor, self.field_name.lower(), obj_name) code = "%s(%s, %d, %s);" % (emit_util_function, obj_name, nbits, operand_getter) return [' ' + code]
def sms_twilio(data, context): ''' Sends an SMS message via Twilio to the given number. The message is "[title] message" config must include the strings twilio_accountsid, twilio_authtoken, and twilio_number data['number']: phone number to send SMS message to. data['twilio_accountsid'], data['twilio_authtoken'], data['twilio_number']: optional Twilio configuration context['title']: used in creating SMS message context['message']: used in creating SMS message ''' from config import config from twilio.rest import TwilioRestClient if 'number' not in data: util.die('alert_sms_twilio: missing number') config_target = config if 'twilio_accountsid' in data and 'twilio_authtoken' in data and 'twilio_number' in data: config_target = data sms_message = "[%s] %s" % (context['title'], context['message']) client = TwilioRestClient(config_target['twilio_accountsid'], config_target['twilio_authtoken']) message = client.messages.create(body = sms_message, to = data['number'], from_ = config_target['twilio_number'])
def main(): parser = argparse.ArgumentParser() # parameter for the AFL. parser.add_argument( 'AFL', help='Application File Locator value (coded in hexadecimal).') # doctest flag. parser.add_argument('-t', '--test', help='run the doctests and exit.', action='store_true') args = parser.parse_args() test = args.test afl = args.AFL if test: doctest.testmod() return valid_in = validate(afl) if not valid_in: util.die("A valid AFL value should be a multiple of 4 Bytes.") print human(afl) return
def main(): parser = argparse.ArgumentParser() # parameter for the CVM list. parser.add_argument( 'CVM', help='Cardholder Verification Method list (coded in hexadecimal).') # doctest flag. parser.add_argument('-t', '--test', help='run the doctests and exit.', action='store_true') args = parser.parse_args() if args.test: doctest.testmod() return valid_in = validate(args.CVM) if not valid_in: util.die("CVM list must contain 8 bytes for amount x and y and at least"\ " 2 bytes for one CVM") print human(args.CVM) return
def check_required(self, path): ok = 1 for name_reqd, val_reqd in self.reqd: if not self.__data.has_key(name_reqd): print "Missing configuration parameter: %s" % name_reqd if name_reqd == 'DENY_THRESHOLD_INVALID': print "\nNote: The configuration parameter DENY_THRESHOLD has been renamed" print " DENY_THRESHOLD_INVALID. Please update your DenyHosts configuration" print " file to reflect this change." if self.__data.has_key('DENY_THRESHOLD'): print "\n*** Using deprecated DENY_THRESHOLD value for DENY_THRESHOLD_INVALID ***" self.__data['DENY_THRESHOLD_INVALID'] = self.__data['DENY_THRESHOLD'] else: ok = 0 elif name_reqd == 'DENY_THRESHOLD_RESTRICTED': print "\nNote: DENY_THRESHOLD_RESTRICTED has not been defined. Setting this" print "value to DENY_THRESHOLD_ROOT" self.__data['DENY_THRESHOLD_RESTRICTED'] = self.__data['DENY_THRESHOLD_ROOT'] else: ok = 0 #elif val_reqd and not self.__data[name_reqd]: elif val_reqd and not self.__data[name_reqd] and not name_reqd in self.to_int: print "Missing configuration value for: %s" % name_reqd ok = 0 if not ok: die("You must correct these problems found in: %s" % path)
def main(): parser = argparse.ArgumentParser() parser.add_argument("decline", help="value of the decline xIAC (e.g:FC0011AB00)") parser.add_argument("online", help="value of the online xIAC (e.g:FC0011AB00)") parser.add_argument("default", help="value of the decline xIAC (e.g:FC0011AB00)") parser.add_argument("-t", "--test", help="run the doctests", default=False, action="store_true") args = parser.parse_args() if args.test: import doctest doctest.testmod() return # xIAC decline = util.rm(args.decline) online = util.rm(args.online) default = util.rm(args.default) valid_in = validate(decline, online, default) if not valid_in: util.die("xIAC must all have the same length and be HEX values.") print human(decline, online, default) return
def main(): parser = argparse.ArgumentParser() # parameter for the TLV. parser.add_argument('TLV', help='TLV string (coded in hexadecimal).') # doctest flag. parser.add_argument('-t', '--test', help='run the doctests and exit.', action='store_true') args = parser.parse_args() if args.test: doctest.testmod() return if not validate(args.TLV): util.die("A valid TLV value must be encoded in HEX and be an integer"\ "multiple of Bytes greater then 0") print human(args.TLV) return
def main(): parser = argparse.ArgumentParser() # parameter for the AIP. parser.add_argument( 'AIP', help='Application Interchange Profile (coded in hexadecimal).' ) # doctest flag. parser.add_argument( '-t', '--test', help='run the doctests and exit.', action='store_true' ) args = parser.parse_args() if args.test: doctest.testmod() return valid_in = validate(args.AIP) if not valid_in: util.die("a valid AIP has a lenght of 2 Bytes and is code in HEX") print human(args.AIP) return
def main(): parser = argparse.ArgumentParser() # parameter for the AIP. parser.add_argument( 'AIP', help='Application Interchange Profile (coded in hexadecimal).') # doctest flag. parser.add_argument('-t', '--test', help='run the doctests and exit.', action='store_true') args = parser.parse_args() if args.test: doctest.testmod() return valid_in = validate(args.AIP) if not valid_in: util.die("a valid AIP has a lenght of 2 Bytes and is code in HEX") print human(args.AIP) return
def check_required(self, path): ok = 1 for name_reqd, val_reqd in self.reqd: if not self.__data.has_key(name_reqd): print "Missing configuration parameter: %s" % name_reqd if name_reqd == 'DENY_THRESHOLD_INVALID': print "\nNote: The configuration parameter DENY_THRESHOLD has been renamed" print " DENY_THRESHOLD_INVALID. Please update your DenyHosts configuration" print " file to reflect this change." if self.__data.has_key('DENY_THRESHOLD'): print "\n*** Using deprecated DENY_THRESHOLD value for DENY_THRESHOLD_INVALID ***" self.__data['DENY_THRESHOLD_INVALID'] = self.__data[ 'DENY_THRESHOLD'] else: ok = 0 elif name_reqd == 'DENY_THRESHOLD_RESTRICTED': print "\nNote: DENY_THRESHOLD_RESTRICTED has not been defined. Setting this" print "value to DENY_THRESHOLD_ROOT" self.__data['DENY_THRESHOLD_RESTRICTED'] = self.__data[ 'DENY_THRESHOLD_ROOT'] else: ok = 0 elif val_reqd and not self.__data[name_reqd]: print "Missing configuration value for: %s" % name_reqd ok = 0 if not ok: die("You must correct these problems found in: %s" % path)
def main(): parser = argparse.ArgumentParser() # parameter for the AFL. parser.add_argument( 'AFL', help='Application File Locator value (coded in hexadecimal).' ) # doctest flag. parser.add_argument( '-t', '--test', help='run the doctests and exit.', action='store_true' ) args = parser.parse_args() test = args.test afl = args.AFL if test: doctest.testmod() return valid_in = validate(afl) if not valid_in: util.die("A valid AFL value should be a multiple of 4 Bytes.") print human(afl) return
def main(): global projectshort # No emails for a repository in the process of being imported git_dir = git.rev_parse(git_dir=True, _quiet=True) if os.path.exists(os.path.join(git_dir, 'pending')): return projectshort = get_module_name() def get_config(hook, skip=False): hook_val = None try: hook_val = git.config(hook, _quiet=True) except CalledProcessError: pass if not hook_val and not skip: die("%s is not set" % hook) return hook_val global debug if (len(sys.argv) > 1): debug = True print "Debug Mode on" else: debug = False recipients = get_config("hooks.mailinglist", debug) smtp_host = get_config("hooks.smtp-host", debug) smtp_port = get_config("hooks.smtp-port", True) smtp_sender = get_config("hooks.smtp-sender", debug) smtp_sender_user = get_config("hooks.smtp-sender-username", True) smtp_sender_pass = get_config("hooks.smtp-sender-password", True) changes = [] if len(sys.argv) > 1: # For testing purposes, allow passing in a ref update on the command line if len(sys.argv) != 4: die("Usage: generate-commit-mail OLDREV NEWREV REFNAME") changes.append(make_change(recipients, smtp_host, smtp_port, smtp_sender, smtp_sender_user, smtp_sender_pass, sys.argv[1], sys.argv[2], sys.argv[3])) else: for line in sys.stdin: items = line.strip().split() if len(items) != 3: die("Input line has unexpected number of items") changes.append(make_change(recipients, smtp_host, smtp_port, smtp_sender, smtp_sender_user, smtp_sender_pass, items[0], items[1], items[2])) for change in changes: all_changes[change.refname] = change for change in changes: change.prepare() change.send_emails() processed_changes[change.refname] = change
def _check_value(value: str, maximum: int): try: amount = int(value) if 0 <= amount <= maximum: return amount die(f'Error: value should be 0 <= (value) <= {maximum}') except ValueError: die(f'Error: value should be integer')
def environ_sub(self, value): while True: environ_match = ENVIRON_REGEX.search(value) if not environ_match: return value name = environ_match.group("environ") env = os.environ.get(name) if not env: die("Could not find environment variable: %s" % name) value = ENVIRON_REGEX.sub(env, value)
def start(): logging.info("Starting") secrets = lockerfs.loadJsonFile("secrets.json") app.client = client.Client(app.info, jid=secrets["jid"], password=secrets["password"]) if app.client.connect(): app.client.process(threaded=True) app.started = True else: util.die("XMPP connection failed")
def check_obsolete_arguments(args): """ give helpful message for obsolete arguments then die """ for (argument, parameter_name) in PARAMETERS_MATCHING_OBSOLETE_ARGUMENTS: if getattr(args, argument, None) is not None: print(getattr(args, argument)) die(f"argument '{argument}' no longer supported, instead use -P to specify an equivalent value for parameter '{parameter_name}'" )
def main(): """ Handle command line arguments and launch curses display, then enter curses_main() """ epilog = """ Reads MSAs in any format supported by BioPython. Autodetects alignments in FASTA, Stockholm, Phylip and Nexus formats. Current does not autodetect PIR format correctly. By default, all gaps are displayed as '.' To preserve the original gap symbols from the alignment file, use the --gapsym option. """ description = """ Terminal-based multiple sequence alignment viewer. """ parser = argparse.ArgumentParser(epilog=epilog, description=description) parser.add_argument('aln_file', help="Path to alignment file.") parser.add_argument('--format', '-f', help="MSA format (skip autodetection)") parser.add_argument('--gapsym', help="Preserve gap symbols from file.", action='store_true', default=False) parser.add_argument('--nucleotide', '-n', action='store_true', default=False, help="Nucleotide alignment.") args = parser.parse_args() if args.format is None: args.format = guess_format(args.aln_file) if args.format is None: die( "FATAL: can't determine format of %s. Try specifying the " "alignment format manually.\n" % args.aln_file, None) try: stdscr=curses.initscr() curses.noecho() curses.cbreak() stdscr.keypad(1) if curses.has_colors(): curses.start_color() curses_main(stdscr, args) # don't print a stack trace if killed by, e.g, SIGINT # we just want to clean up the environment and quit except KilledException: pass finally: stdscr.erase() stdscr.refresh() stdscr.keypad(0) curses.echo() curses.nocbreak() curses.endwin() return 0
def ssl_expire(data): # keys: hostname, optional port (default 443), days (default 7), and timeout (default 10) from config import config if 'hostname' not in data: util.die('checks.ssl_expire: missing hostname') hostname = data['hostname'] port = int(extract(data, 'port', 443)) days = int(extract(data, 'days', 7)) timeout = float(extract(data, 'timeout', 10)) import ssl from datetime import datetime import socket try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(timeout) sock.connect((hostname, port)) ssl_sock = ssl.wrap_socket( sock, cert_reqs=ssl.CERT_REQUIRED, ca_certs='/etc/ssl/certs/ca-certificates.crt', ciphers=("HIGH:-aNULL:-eNULL:-PSK:RC4-SHA:RC4-MD5")) cert = ssl_sock.getpeercert() ssl_sock.close() except ssl.SSLError as e: return { 'status': 'fail', 'message': 'SSL connection failure: %s' % (e.strerror) } try: expire_date = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z') except: return { 'status': 'fail', 'message': 'Certificate has unknown date format: %s' % (cert['notAfter']) } expire_in = expire_date - datetime.now() if expire_in.days < days: return { 'status': 'fail', 'message': 'SSL certificate will expire in %d hours' % (expire_in.total_seconds() / 3600, ) } else: return {'status': 'success'}
def balance(args): icon_service = get_icon_service(args.endpoint) if args.keystore: address = get_address_from_keystore(args.keystore) elif args.address: address = args.address else: die('Error: keystore or address should be specified') _balance = icon_service.get_balance(address) print('ICX =', in_icx(_balance))
def get_str_value(self): if self.is_field_binding() or self.is_return(): return self.value if self.is_nonterminal(): return self.nt if self.is_ntluf(): return self.ntluf err = "unsupported type: %s for function get_str_value" % self.type util.die(err)
def _claim_iscore(self, wallet, address): print('\n>>> Claim IScore:') iscore = IScore(self._icon_service) result = iscore.query(address) iscore.print_status(address, result) estimated_icx = in_icx(int(result['estimatedICX'], 16)) if (estimated_icx - 1.0) <= 0: die('Error: EstimatedICX should be larger than 1.0') tx_hash = iscore.claim(wallet) self._ensure_tx_result(tx_hash, True)
def get_config(hook, skip=False): hook_val = None try: hook_val = git.config(hook, _quiet=True) except CalledProcessError: pass if not hook_val and not skip: die("%s is not set" % hook) return hook_val
def __init__(self, name, aggtype, ctype, bitwidth, default_visibility=None, default_initializer=None, xprint='NOPRINT', internal_or_public="INTERNAL", dio="DO", eio="EO",): self.name = name self.aggtype = aggtype self.ctype = ctype self.bitwidth = int(bitwidth) self.default_visibility = default_visibility self.xprint = xprint self.internal_or_public = internal_or_public self.dio = dio self.eio = eio if self.eio in ['EI', 'EO']: pass else: err = "Bad Encoder IO value: %s -- need one of {EI,EO}" util.die(err % self.eio) if self.dio in ['DI', 'DO', 'DS']: pass else: err = "Bad decoder IO value: %s -- need one of {DI,DO,DS}" util.die(err % self.eio) if self.eio == 'EI': self.encoder_input = True else: self.encoder_input = False if self.dio == 'DS': self.decoder_skip = True else: self.decoder_skip = False # NOTE: this next field is only used if initialize_each_field is True. self.default_initializer = default_initializer self.is_enum = 'enum' in self.ctype # this is the C type that will be used in the operand storage struct. self.storage_type = None # if True using bit fields self.compressed = False
def remove(self, die_=True): try: if self.fd: os.close(self.fd) except: pass self.fd = None try: os.unlink(self.lockpath) except Exception, e: if die_: die("Error deleting DenyHosts lock file: %s" % self.lockpath, e)
def start(secrets): logging.info("Starting") app.client = client.Client(app.info, jid=secrets["jid"], password=secrets["password"]) address = (secrets["host"], secrets["port"]) if (secrets.has_key("host") and secrets.has_key("port")) else () logging.info("XMPP connecting with address " + str(address)) if app.client.connect(address): app.client.process(threaded=True) app.started = True else: # XXX We shouldn't die here, we should still serve existing data and try again. # We could also prompt for credentials again util.die("XMPP connection failed")
def create(self): try: self.fd = os.open(self.lockpath, os.O_CREAT | # create file os.O_TRUNC | # truncate it, if it exists os.O_WRONLY | # write-only os.O_EXCL, # exclusive access 0644) # file mode except Exception, e: pid = self.get_pid() die("DenyHosts could not obtain lock (pid: %s)" % pid, e)
def ch_def(): """ Returns the default channel with which communication with the card will occur. """ try: reader_def = reader_name_def() t = terminal_select(reader_def) card = t.connect(PROTO_DEF) ch = card.getBasicChannel() return ch except: util.die("could not get a connection to the card with the default terminal.")
def handle_change(self, thread_name, check_id, check_name, lock_uid, status, check_result): if status == 'offline': import subprocess googleResult = subprocess.Popen( ['ping', 'ipv4.google.com', '-c', '3', '-w', '3'], stdout=subprocess.PIPE).stdout.read() if '100% packet loss' in googleResult: print "What?! Google down? Aborting alert sending as it seems like the monitor host has no connectivity" return updown = 'down' elif status == 'online': updown = 'up' safe_print("[%s] ... confirmed, target is %s state now", (thread_name, status)) update_result = database.query( "UPDATE checks SET status = %s, confirmations = 0, `lock` = '', last_checked = NOW() WHERE id = %s AND `lock` = %s", (status, check_id, lock_uid)) if update_result.rowcount == 1: # we still had the lock at the point where status was toggled # then, send the alert alert_result = database.query( "SELECT contacts.id, contacts.type, contacts.data FROM contacts, alerts WHERE contacts.id = alerts.contact_id AND alerts.check_id = %s AND alerts.type IN ('both', %s)", (check_id, updown)) for alert_row in alert_result.fetchall(): safe_print("[%s] ... alerting contact %d", (thread_name, alert_row['id'])) alert_func = getattr(alerts, alert_row['type'], None) if not alert_func: util.die("Invalid alert handler [%s]!" % (alert_row['type'])) # build context context = {} context['check_id'] = check_id context['check_name'] = check_name context['contact_id'] = alert_row['id'] context['title'] = "Check %s: %s" % (status, check_name) context['status'] = status context['updown'] = updown context['message'] = check_result['message'] alert_func(util.decode(alert_row['data']), context) # also add an event database.query( "INSERT INTO check_events (check_id, type) VALUES (%s, %s)", (check_id, updown))
def http_status(data): # keys: status, url (also http_helper params) if 'status' not in data or 'url' not in data: util.die('checks.http_status: missing status') result = http_helper(data) if result['status'] == 'fail': return result elif str(result['code']) == data['status']: return {'status': 'success'} else: return {'status': 'fail', 'message': "target [%s] returned unexpected status [%s], expected [%s]" % (data['url'], str(result['code']), data['status'])}
def http_contains(data): # keys: substring, url (also http_helper params) if 'substring' not in data or 'url' not in data: util.die('checks.http_contains: missing substring') result = http_helper(data) if result['status'] == 'fail': return result elif data['substring'] in result['content']: return {'status': 'success'} else: return {'status': 'fail', 'message': "target [%s] does not contain string [%s]" % (data['url'], data['substring'])}
def classify(self, doc): model_file_path = self.path + "svm_model" model_file = '%ssvm_model' % self.path if not self.feats: util.die('Incomplete model') if not os.path.isfile(model_file): util.die('no model [%s]' % model_file) ## testing data file sys.stderr.write('SVM classifying... ') lines = [] frag = doc.frag while frag: if frag.label == None: svm_label = '0' elif frag.label: svm_label = '+1' else: svm_label = '-1' line = '%s ' % svm_label feats = [f + '_' + v for f, v in frag.features.items()] svm_feats = [self.feats[f] for f in feats if f in self.feats] svm_feats.sort(lambda x, y: x - y) line += ' '.join(['%d:1' % x for x in svm_feats]) lines.append(line) frag = frag.next unused, test_file = tempfile.mkstemp() fh = open(test_file, 'w') fh.write('\n'.join(lines) + '\n') fh.close() ## classify test data unused, pred_file = tempfile.mkstemp() options = '-v 0' #cmd = '%s %s %s %s %s' %(SVM_CLASSIFY, options, test_file, model_file, pred_file) #cmd = SVM_CLASSIFY + " " +options + " " +test_file + " " + model_file + " " + pred_file cmd = "\"" + SVM_CLASSIFY + "\" " + options + " \"" + test_file + "\" \"" + model_file_path + "\" \"" + pred_file + "\"" print cmd os.system(cmd) ## get predictions total = 0 preds = map(float, open(pred_file).read().splitlines()) frag = doc.frag while frag: frag.pred = util.logit(preds[total]) frag = frag.next total += 1 ## clean up #os.remove(test_file) #os.remove(pred_file) sys.stderr.write('done!\n')
def telegram_sender(data, context): ''' Notifies a web hook over HTTP regarding alert. All context parameters are sent, via GET. data['url']: the target web hook context: encoded as GET parameters ''' if 'token' not in data and 'chat_id' not in data: util.die('telegram_sender: missing token or chat_id') from telegram import Bot bot = Bot(data['token']) bot.sendMessage(chat_id=data['chat_id'], text=context['message'])
def ch_def(): """ Returns the default channel with which communication with the card will occur. """ try: reader_def = reader_name_def() t = terminal_select(reader_def) card = t.connect(PROTO_DEF) ch = card.getBasicChannel() return ch except: util.die( "could not get a connection to the card with the default terminal." )
def ping(data): # keys: target if 'target' not in data: util.die('checks.ping: missing target') import subprocess target = data['target'] result = subprocess.Popen(['ping', target, '-c', '3', '-w', '3'], stdout=subprocess.PIPE).stdout.read() if '100% packet loss' in result: return {'status': 'fail', 'message': "No response from %s" % (target,)} else: return {'status': 'success'}
def classify(self, doc): model_file_path = self.path + "svm_model" model_file = '%ssvm_model' %self.path if not self.feats: util.die('Incomplete model') if not os.path.isfile(model_file): util.die('no model [%s]' %model_file) ## testing data file sys.stderr.write('SVM classifying... ') lines = [] frag = doc.frag while frag: if frag.label == None: svm_label = '0' elif frag.label: svm_label = '+1' else: svm_label = '-1' line = '%s ' %svm_label feats = [f+'_'+v for f,v in frag.features.items()] svm_feats = [self.feats[f] for f in feats if f in self.feats] svm_feats.sort(lambda x,y: x-y) line += ' '.join(['%d:1' %x for x in svm_feats]) lines.append(line) frag = frag.next unused, test_file = tempfile.mkstemp() fh = open(test_file, 'w') fh.write('\n'.join(lines) + '\n') fh.close() ## classify test data unused, pred_file = tempfile.mkstemp() options = '-v 0' #cmd = '%s %s %s %s %s' %(SVM_CLASSIFY, options, test_file, model_file, pred_file) #cmd = SVM_CLASSIFY + " " +options + " " +test_file + " " + model_file + " " + pred_file cmd = "\""+SVM_CLASSIFY + "\" " +options + " \"" +test_file + "\" \"" + model_file_path + "\" \"" + pred_file+"\"" print cmd os.system(cmd) ## get predictions total = 0 preds = map(float, open(pred_file).read().splitlines()) frag = doc.frag while frag: frag.pred = util.logit(preds[total]) frag = frag.next total += 1 ## clean up #os.remove(test_file) #os.remove(pred_file) sys.stderr.write('done!\n')
def get_module_name(): try: git_dir = git.rev_parse(git_dir=True, _quiet=True) except CalledProcessError: die("GIT_DIR not set") # Use the directory name with .git stripped as a short identifier absdir = os.path.abspath(git_dir) if absdir.endswith(os.sep + '.git'): absdir = os.path.dirname(absdir) projectshort = os.path.basename(absdir) if projectshort.endswith(".git"): projectshort = projectshort[:-4] return projectshort
def _guess_os(): s = platform.system() bits = platform.architecture()[0] if s == 'Darwin': return OSX elif s == 'Linux': if bits == '32bit': return LINUX32 elif bits == '64bit': return LINUX64 else: die(u"Unrecognized Linux bit information; " + \ u"expected '32bit' or '64bit' but got: %s" % bits) elif s == 'Windows': # tons of people use windows 7, and some of them might figure # out what to do, so pretend we're on win32 and let them try. # TODO some warning would be good return WINXP32
def tcp_connect(data): # keys: target, port; optional: timeout if 'target' not in data or 'port' not in data: util.die('checks.tcp_connect: missing target or port') target = data['target'] port = int(data['port']) timeout = float(extract(data, 'timeout', 5)) import socket sock = socket.socket() sock.settimeout(timeout) sock.connect((target, port)) sock.close() return {'status': 'success'}
def train(self, doc): """ takes training data and a path and creates an svm model """ model_file = '%ssvm_model' %self.path ## need integer dictionary for features sys.stderr.write('training. making feat dict... ') feat_list = set() frag = doc.frag while frag: feats = [f+'_'+v for f,v in frag.features.items()] for feat in feats: feat_list.add(feat) frag = frag.next self.feats = dict(zip(feat_list, range(1,len(feat_list)+1))) ## training data file sys.stderr.write('writing... ') lines = [] frag = doc.frag while frag: if frag.label == None: util.die('expecting labeled data') elif frag.label > 0.5: svm_label = '+1' elif frag.label < 0.5: svm_label = '-1' else: continue line = '%s ' %svm_label feats = [f+'_'+v for f,v in frag.features.items()] svm_feats = [self.feats[f] for f in feats] svm_feats.sort(lambda x,y: x-y) line += ' '.join(['%d:1' %x for x in svm_feats]) lines.append(line) frag = frag.next unused, train_file = tempfile.mkstemp() fh = open(train_file, 'w') fh.write('\n'.join(lines) + '\n') fh.close() ## train an svm model sys.stderr.write('running svm... ') options = '-c 1 -v 0' cmd = '%s %s %s %s' %(SVM_LEARN, options, train_file, model_file) os.system(cmd) sys.stderr.write('done!\n') ## clean up os.remove(train_file)
def worker(self): thread_name = threading.currentThread().getName() while True: check_id, check_name, check_type, check_data, status, max_confirmations, confirmations, lock_uid = self.q.get() safe_print("[%s] processing check %d: calling checks.%s", (thread_name, check_id, check_type)) check_result = checks.run_check(check_type, util.decode(check_data), check_id) safe_print("[%s] check %d result: %s", (thread_name, check_id, str(check_result))) if not type(check_result) is dict or 'status' not in check_result: util.die("[%s] bad check handler [%s]: returned non-dict or missing status" % (thread_name, check_type)) elif 'message' not in check_result: if check_result['status'] == 'fail': check_result['message'] = "Check offline: %s" % (check_name) else: check_result['message'] = "Check online: %s" % (check_name) if check_result['status'] == 'fail': safe_print("[%s] ... got failure!", (thread_name)) if status == 'online': with recent_failures_lock: recent_failures.add((check_id, util.time())) if confirmations + 1 >= max_confirmations: # target has failed self.handle_change(thread_name, check_id, check_name, lock_uid, 'offline', check_result) else: # increase confirmations database.query("UPDATE checks SET confirmations = confirmations + 1, `lock` = '', last_checked = NOW() WHERE id = %s AND `lock` = %s", (check_id, lock_uid)) else: database.query("UPDATE checks SET confirmations = 0, `lock` = '', last_checked = NOW() WHERE id = %s AND `lock` = %s", (check_id, lock_uid)) elif check_result['status'] == 'success': safe_print("[%s] ... got success", (thread_name)) if status == 'offline': if confirmations + 1 >= max_confirmations: # target has come back online self.handle_change(thread_name, check_id, check_name, lock_uid, 'online', check_result) else: # increase confirmations database.query("UPDATE checks SET confirmations = confirmations + 1, `lock` = '', last_checked = NOW() WHERE id = %s AND `lock` = %s", (check_id, lock_uid)) else: database.query("UPDATE checks SET confirmations = 0, `lock` = '', last_checked = NOW() WHERE id = %s AND `lock` = %s", (check_id, lock_uid)) else: util.die("Check handler [%s] returned invalid status code [%s]!") % (check_type, check_result['status'])
def get_committer_email(rev): try: git_email = git.show('--raw', '--pretty=format:%an <%aE>', rev) except CalledProcessError: die("GIT_ERROR retrieving email") if git_email is None: return None if len(git_email.strip()) == 0: return None array = git_email.strip().splitlines() if len(array) == 0: return None return array[0]
def get_project_description(): try: git_dir = git.rev_parse(git_dir=True, _quiet=True) except CalledProcessError: die("GIT_DIR not set") projectdesc = '' description = os.path.join(git_dir, 'description') if os.path.exists(description): try: projectdesc = open(description).read().strip() except: pass if projectdesc.startswith('Unnamed repository;'): projectdesc = '' return projectdesc
def http_helper(data): from config import config # keys: url, timeout if 'url' not in data: util.die('checks.http_helper: missing url') import httplib2 try: handle = httplib2.Http(timeout = float(extract(data, 'timeout', 10))) resp, content = handle.request(data['url'], 'GET', headers={'User-Agent': config['user_agent']}) return {'status': 'success', 'code': resp.status, 'content': content} except httplib2.RelativeURIError as e: return {'status': 'fail', 'message': 'RelativeURIError (possibly invalid URI: %s)' % (data['url'])} except httplib2.HttpLib2Error as e: return {'status': 'fail', 'message': e.strerror}