def chloglevel(signum,frame): if signum == signal.SIGUSR1: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_WARNING)) syslog.syslog(syslog.LOG_WARNING,"Signal received ("+str(signum)+"). Setting logging level to WARNING.") if signum == signal.SIGUSR2: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) syslog.syslog(syslog.LOG_INFO,"Signal received ("+str(signum)+"). Setting logging level to INFO.")
def __init__(self, priority = message.INFO, max_length = 10000): logger.__init__(self, priority, max_length) if os.name == 'posix': import syslog self.syslog = syslog.syslog syslog.openlog('p4dti', syslog.LOG_PID, syslog.LOG_DAEMON) syslog.setlogmask(syslog.LOG_UPTO(priority))
def __init__(self, options, args): """Initialize an instance of StdEngine. options: An object containing values for all options, as returned by optparse args: The command line arguments, as returned by optparse. """ config_dict = self.getConfiguration(options, args) # Set a default socket time out, in case FTP or HTTP hang: timeout = int(config_dict.get('socket_timeout', 20)) socket.setdefaulttimeout(timeout) syslog.openlog('weewx', syslog.LOG_PID|syslog.LOG_CONS) # Look for the debug flag. If set, ask for extra logging weewx.debug = int(config_dict.get('debug', 0)) if weewx.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) # Set up the weather station hardware: self.setupStation(config_dict) # Set up the services to be run: self.setupServices(config_dict)
def open(self, verbosity=syslog.LOG_NOTICE): log_options = syslog.LOG_PID | syslog.LOG_CONS | syslog.LOG_NDELAY if self.options.debug: log_options |= syslog.LOG_PERROR syslog.openlog("conreality", logoption=log_options, facility=syslog.LOG_DAEMON) syslog.setlogmask(syslog.LOG_UPTO(verbosity)) return self
def configDatabase(config_dict, binding, start_ts=start_ts, stop_ts=stop_ts, interval=interval,amplitude=1.0, day_phase_offset=0.0, annual_phase_offset=0.0, weather_phase_offset=0.0): """Configures the archive databases.""" global schema # Check to see if it already exists and is configured correctly. try: with weewx.manager.open_manager_with_config(config_dict, binding) as archive: if archive.firstGoodStamp() == start_ts and archive.lastGoodStamp() == stop_ts: # Database already exists. We're done. return except weedb.DatabaseError: pass # Delete anything that might already be there. try: weewx.manager.drop_database_with_config(config_dict, binding) except weedb.DatabaseError: pass # Need to build a new synthetic database. General strategy is to create the # archive data, THEN backfill with the daily summaries. This is faster than # creating the daily summaries on the fly. # First, we need to modify the configuration dictionary that was passed in # so it uses the DBManager, instead of the daily summary manager monkey_dict = config_dict.dict() monkey_dict['DataBindings'][binding]['manager'] = 'weewx.manager.Manager' with weewx.manager.open_manager_with_config(monkey_dict, binding, initialize=True) as archive: # Because this can generate voluminous log information, # suppress all but the essentials: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) # Now generate and add the fake records to populate the database: t1= time.time() archive.addRecord(genFakeRecords(start_ts=start_ts, stop_ts=stop_ts, interval=interval, amplitude=amplitude, day_phase_offset=day_phase_offset, annual_phase_offset=annual_phase_offset, weather_phase_offset=weather_phase_offset)) t2 = time.time() print "\nTime to create synthetic archive database = %6.2fs" % (t2-t1,) with weewx.manager.open_manager_with_config(config_dict, binding, initialize=True) as archive: # Now go back to regular logging: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Backfill with daily summaries: t1 = time.time() nrecs, ndays = archive.backfill_day_summary() tdiff = time.time() - t1 if nrecs: print "\nProcessed %d records to backfill %d day summaries in %.2f seconds" % (nrecs, ndays, tdiff) else: print "Daily summaries up to date."
def setUp(self): global config_path global cwd weewx.debug = 1 syslog.openlog('test_stats', syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Save and set the current working directory in case some service changes it. if not cwd: cwd = os.getcwd() else: os.chdir(cwd) try : config_dict = configobj.ConfigObj(config_path, file_error=True) except IOError: sys.stderr.write("Unable to open configuration file %s" % self.config_path) # Reraise the exception (this will eventually cause the program to exit) raise except configobj.ConfigObjError: sys.stderr.write("Error while parsing configuration file %s" % config_path) raise self.archive_db_dict = config_dict['Databases'][self.archive_db] self.stats_db_dict = config_dict['Databases'][self.stats_db] # This will generate the test databases if necessary: gen_fake_data.configDatabases(self.archive_db_dict, self.stats_db_dict)
def setUp(self): global config_path global cwd weewx.debug = 1 syslog.openlog('test_templates', syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Save and set the current working directory in case some service changes it. if not cwd: cwd = os.getcwd() else: os.chdir(cwd) try : self.config_dict = configobj.ConfigObj(config_path, file_error=True) except IOError: sys.stderr.write("Unable to open configuration file %s" % config_path) # Reraise the exception (this will eventually cause the program to exit) raise except configobj.ConfigObjError: sys.stderr.write("Error while parsing configuration file %s" % config_path) raise # Remove the old directory: try: test_html_dir = os.path.join(self.config_dict['WEEWX_ROOT'], self.config_dict['StdReport']['HTML_ROOT']) shutil.rmtree(test_html_dir) except OSError, e: if os.path.exists(test_html_dir): print >> sys.stderr, "\nUnable to remove old test directory %s", test_html_dir print >> sys.stderr, "Reason:", e print >> sys.stderr, "Aborting" exit(1)
def configure(self, config_dict): parser = self.get_parser() self.add_options(parser) options, _ = parser.parse_args() if options.debug is not None: weewx.debug = options.debug syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) prompt = False if options.noprompt else True self.do_options(options, parser, config_dict, prompt)
def gen_all(config_path, gen_ts = None): syslog.openlog('reports', syslog.LOG_PID|syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) socket.setdefaulttimeout(10) t = weewx.reportengine.StdReportEngine(config_path, gen_ts) t.start() t.join()
def set_level(self, level): levels = { 'error': syslog.LOG_ERR, 'warning': syslog.LOG_WARNING, 'info': syslog.LOG_INFO, 'debug': syslog.LOG_DEBUG, } try: syslog.setlogmask(syslog.LOG_UPTO(levels[level])) except KeyError: raise LoggerError("Unknown log level while setting threshold")
def setup_syslog(dest, debug=False): if dest == 'console': prio = dict((v, k[4:]) for k, v in vars(syslog).items() if type(v) == int and (v & 7) > 0 and k not in ('LOG_PID', 'LOG_CONS')) syslog.syslog = lambda *m: sys.stdout.write('%s %8s: %s\n' % (str(datetime.datetime.now()), prio[m[0]], m[1])) if m[0] != syslog.LOG_DEBUG or debug else True elif dest is not None: syslog.openlog(ident=os.path.basename(sys.argv[0]), logoption=syslog.LOG_PID, facility=getattr(syslog, 'LOG_%s' % dest.upper())) else: syslog.syslog = lambda *m: True syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG if debug else syslog.LOG_INFO)) syslog.syslog(syslog.LOG_DEBUG, 'logging started')
def updateFlybase(): ''' Updates genes in a MySQL database with Flybase info. ''' syslog.openlog("FlybaseUpdater.info", 0, syslog.LOG_USER) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_NOTICE)) syslog.syslog(syslog.LOG_NOTICE, "Starting up.") try: dbConn = DbConn(MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE) if not dbConn: syslog.syslog(syslog.LOG_CRIT, "Unable to connect to MySQL. Exiting.") return # Now loop every N minutes, updating flybase info. while 1: syslog.syslog(syslog.LOG_DEBUG, "Starting a loop.") try: loopStartTime = time.time() parallelcurl = pyparallelcurl.ParallelCurl(1) # load all genes (along with alias lists) into memory. syslog.syslog(syslog.LOG_DEBUG, "Loading gene info.") geneCursor = dbConn.queryDB(u'''SELECT `id`, `name`, `flybase_id`, `cg_id`, `isoform_image_path` FROM `transcription_factors` WHERE (`flybase_id` IS NOT NULL && `flybase_id` != '')''') gene = geneCursor.fetchone() while gene is not None: # assemble a list of aliases for this gene. geneInfo = {'id': int(gene[0]), 'name': str(gene[1]), 'flybase_id': str(gene[2]), 'cg_id': str(gene[3]), 'isoform_image_path': str(gene[4]), 'aliases': {}, 'isoforms': {}} geneAliasQuery = dbConn.queryDB(u'''SELECT `id`, `name` FROM `aliases` WHERE `transcription_factor_id` = %s''', params=[str(gene[0])], newCursor=True) alias = geneAliasQuery.fetchone() while alias is not None: geneInfo['aliases'][str(alias[1])] = int(alias[0]) alias = geneAliasQuery.fetchone() # assemble a list of isoforms for this gene. geneIsoformQuery = dbConn.queryDB(u'''SELECT `id`, `name`, `flybase_id`, `refseq_id` FROM `isoforms` WHERE `transcription_factor_id` = %s''', params=[str(gene[0])], newCursor=True) isoform = geneIsoformQuery.fetchone() while isoform is not None: geneInfo['isoforms'][isoform[1].encode('utf-8')] = {'id': int(isoform[0]), 'name': isoform[1].encode('utf-8'), 'flybase_id': isoform[2].encode('utf-8'), 'refseq_id': isoform[3].encode('utf-8')} isoform = geneIsoformQuery.fetchone() # submit this request to parallelcurl object to be compared. syslog.syslog(syslog.LOG_DEBUG, "Submitting request for " + gene[1]) parallelcurl.startrequest('http://flybase.org/reports/'+ str(gene[2]) +'.html', compareGeneFlybaseInfo, {'geneDBInfo': geneInfo, 'dbConn': dbConn}) gene = geneCursor.fetchone() parallelcurl.finishallrequests() except: syslog.syslog(syslog.LOG_ERR, "Recoverable error:\n" + str("".join(traceback.format_exc())) + "\n") syslog.syslog(syslog.LOG_NOTICE, "Loop finished. Sleeping for " + str(LOOP_EVERY_MINUTES) + " minutes.") time.sleep(LOOP_EVERY_MINUTES * 60) except: syslog.syslog(syslog.LOG_CRIT, "Fatal error:\n" + str("".join(traceback.format_exc())) + "\n")
def __init__(self, log_facility, log_level_str): try: syslog.openlog(logoption=syslog.LOG_PID, facility=log_facility) except IOError: try: syslog.openlog(syslog.LOG_PID, log_facility) except IOError: try: syslog.openlog('swiftlm', syslog.LOG_PID, log_facility) except: raise syslog.setlogmask(syslog.LOG_UPTO(SYSLOG_LEVEL_MAP[log_level_str])) logging.Handler.__init__(self)
def get_loglevel(): mask = syslog.setlogmask(0) for level in (syslog.LOG_DEBUG, syslog.LOG_INFO, syslog.LOG_NOTICE, syslog.LOG_WARNING, syslog.LOG_ERR, syslog.LOG_CRIT, syslog.LOG_ALERT, syslog.LOG_EMERG): if syslog.LOG_MASK(level) & mask: return level
def main(): # (1) ident, i.e., identity: the name of the program which will write the logging messages to syslog and will be # prepended to every message. It defaults to sys.argv[0] with leading path components stripped if not set. # (2) logoption: options for opening the connection or writing messages which can be an OR of multiple options, # e.g., LOG_PID -- include PID with each message; # LOG_CONS -- write directly to system console if there is an error while sending to system logger. # (3) facility: specify what type of program is logging the message. For user applications, it should be the # default value -- LOG_USER. # For a complete list of logoption and facility values, please refer to syslog help via "man 3 syslog". syslog.openlog(ident = 'Program-Name', logoption = syslog.LOG_PID|syslog.LOG_CONS, facility = syslog.LOG_USER) syslog.syslog(syslog.LOG_INFO, "This is an informational message.") # By default, syslog logs messages of all priorities. The return value of syslog.setlogmask is the old value # of the log mask which is the default value 0xff here. The new value of the log mask is # syslog.LOG_UPTO(syslog.LOG_ERR) = 0x0f. oldLogMask = syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) print "The old priority mask is 0x%x and the new priority mask is 0x%x." \ % (oldLogMask, syslog.LOG_UPTO(syslog.LOG_ERR)) # Since the priority mask is "up to" LOG_ERR, we should see the error message in /var/log/syslog but not the # warning message. syslog.syslog(syslog.LOG_WARNING, "This is a warning message.") syslog.syslog(syslog.LOG_ERR, "This is an error message.") # Reset the syslog module values (e.g., ident, logoption, facility, logmask) and call the system library closelog(). syslog.closelog()
def setUp(self): global config_path global cwd weewx.debug = 1 syslog.openlog('test_sim', syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Save and set the current working directory in case some service changes it. if not cwd: cwd = os.getcwd() else: os.chdir(cwd) try : self.config_dict = configobj.ConfigObj(config_path, file_error=True) except IOError: sys.stderr.write("Unable to open configuration file %s" % config_path) # Reraise the exception (this will eventually cause the program to exit) raise except configobj.ConfigObjError: sys.stderr.write("Error while parsing configuration file %s" % config_path) raise (first_ts, last_ts) = _get_first_last(self.config_dict) # Set the database to be used in the configuration dictionary self.config_dict['StdArchive']['archive_database'] = self.archive_db self.config_dict['StdArchive']['stats_database'] = self.stats_db self.archive_db_dict = self.config_dict['Databases'][self.archive_db] try: with weewx.archive.Archive.open(self.archive_db_dict) as archive: if archive.firstGoodStamp() == first_ts and archive.lastGoodStamp() == last_ts: print "Simulator need not be run" return except weedb.OperationalError: pass # This will generate the simulator data: engine = weewx.wxengine.StdEngine(self.config_dict) try: engine.run() except weewx.StopNow: pass
def main(): syslog.openlog('ws1', syslog.LOG_PID | syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display driver version') parser.add_option('--port', dest='port', metavar='PORT', help='serial port to which the station is connected', default=DEFAULT_PORT) (options, args) = parser.parse_args() if options.version: print "ADS WS1 driver version %s" % DRIVER_VERSION exit(0) with Station(options.port) as s: print s.get_readings()
def init_logging(target=TARGET_CONSOLE,level=DEFAULT_LOG_LEVEL): global global_target global global_level if not type(level) is int: raise RuntimeError("Unknown log level value %s" % level) global_target=target global_level=level if target==TARGET_CONSOLE: pass elif target==TARGET_SYSLOG: syslog.openlog("arpwatch",0,syslog.LOG_USER) syslog.setlogmask(syslog.LOG_UPTO(global_level)) else: raise RuntimeError("Unknown target value")
def loadconfig(): config = ConfigParser.RawConfigParser() try: config.read(config_path) # syslogマスク設定 loglevel = config.get('Global', 'LogLevel') syslog.setlogmask(syslog.LOG_UPTO(LOG[loglevel])) slog(LOG['DEBUG'], "LogLevel: " + loglevel) # その他設定の読み込み global interval, cache, plugindir interval = 24 cache = '/var/cache/ddns' plugindir = os.path.join(os.getcwd(), 'plugin') interval = config.getint('Global', 'UpdateInterval') cache = os.path.normpath(config.get('Global', 'CacheDir')) pdir = config.get('Global', 'PluginDir') if "/" == pdir[0]: plugindir = os.path.normpath(pdir) else: plugindir = os.path.normpath(os.path.join(os.getcwd(), pdir)) slog(LOG['DEBUG'], "UpdateInterval: " + str(interval)) slog(LOG['DEBUG'], "CacheDir: " + cache) slog(LOG['DEBUG'], "PluginDir: " + plugindir) if not os.path.exists(cache): os.makedirs(cache) # IPアドレスチェックサービスリストの読み込み servers = config.items('IPAddrChecker') global ip_checker ip_checker = [] for key,url in servers: ip_checker.append(url) slog(LOG['DEBUG'], "IPChecker: " + url) except ConfigParser.MissingSectionHeaderError: slog(LOG['WARNING'], "MissingSectionHeader") return False except ConfigParser.NoSectionError: slog(LOG['WARNING'], "NoSectionError") return False except ConfigParser.NoOptionError: slog(LOG['WARNING'], "NoOptionError") return False except: slog(LOG['ERR'], "Unexpected error: " + str(sys.exc_info()[:2])) return False return True
def configDatabases(archive_db_dict, stats_db_dict): """Configures the main and stats databases.""" # Check to see if it already exists and is configured correctly. try: with weewx.archive.Archive.open(archive_db_dict) as archive: if archive.firstGoodStamp() == start_ts and archive.lastGoodStamp() == stop_ts: # Database already exists. We're done. return except: pass # Delete anything that might already be there. try: weedb.drop(archive_db_dict) except: pass # Now build a new one: with weewx.archive.Archive.open_with_create(archive_db_dict, user.schemas.defaultArchiveSchema) as archive: # Because this can generate voluminous log information, # suppress all but the essentials: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) # Now generate and add the fake records to populate the database: t1= time.time() archive.addRecord(genFakeRecords()) t2 = time.time() print "Time to create synthetic archive database = %6.2fs" % (t2-t1,) # Now go back to regular logging: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Delete any old stats database: try: weedb.drop(stats_db_dict) except weedb.NoDatabase: pass # Now create and configure a new one: with weewx.stats.StatsDb.open_with_create(stats_db_dict, user.schemas.defaultStatsSchema) as stats: t1 = time.time() # Now backfill the stats database from the main archive database. nrecs = stats.backfillFrom(archive) t2 = time.time() print "Time to backfill stats database with %d records: %6.2fs" % (nrecs, t2-t1)
def setUp(self): global config_path global cwd weewx.debug = 1 syslog.openlog('test_sim', syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Save and set the current working directory in case some service changes it. if not cwd: cwd = os.getcwd() else: os.chdir(cwd) try : self.config_dict = configobj.ConfigObj(config_path, file_error=True) except IOError: sys.stderr.write("Unable to open configuration file %s" % config_path) # Reraise the exception (this will eventually cause the program to exit) raise except configobj.ConfigObjError: sys.stderr.write("Error while parsing configuration file %s" % config_path) raise # Fiddle with config_dict to reflect the database in use: self.config_dict['DataBindings']['wx_binding']['database'] = self.database (first_ts, last_ts) = _get_first_last(self.config_dict) try: with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as dbmanager: if dbmanager.firstGoodStamp() == first_ts and dbmanager.lastGoodStamp() == last_ts: print "\nSimulator need not be run" return except weedb.OperationalError: pass # This will generate the simulator data: engine = weewx.engine.StdEngine(self.config_dict) try: engine.run() except weewx.StopNow: pass
def main(): import optparse syslog.openlog('wee_netatmo', syslog.LOG_PID | syslog.LOG_CONS) parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display driver version') parser.add_option('--debug', dest='debug', action='store_true', help='display diagnostic information while running') parser.add_option('--run-sniff-driver', dest='ts', action='store_true', help='run the driver in packet sniff mode') parser.add_option('--run-cloud-driver', dest='tc', action='store_true', help='run the driver in cloud client mode') parser.add_option('--test-parse', dest='tp', metavar='FILENAME', help='test the tcp packet parser') parser.add_option('--username', dest='username', metavar='USERNAME', help='username for cloud mode') parser.add_option('--password', dest='password', metavar='PASSWORD', help='password for cloud mode') parser.add_option('--client-id', dest='ci', metavar='CLIENT_ID', help='client_id for cloud mode') parser.add_option('--client-secret', dest='cs', metavar='CLIENT_SECRET', help='client_secret for cloud mode') parser.add_option('--get-stn-data', dest='sdata', action='store_true', help='get formatted station data from cloud') parser.add_option('--get-json-data', dest='jdata', action='store_true', help='get all cloud data as json response') (opts, args) = parser.parse_args() if opts.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) if opts.ts: run_packet_driver() if opts.tc: run_cloud_driver(opts.username, opts.password, opts.ci, opts.cs) if opts.tp: test_parse(options.tp) if opts.sdata: get_station_data(opts.username, opts.password, opts.ci, opts.cs) if opts.jdata: get_json_data(opts.username, opts.password, opts.ci, opts.cs)
def main(): import optparse syslog.openlog('wee_owfs', syslog.LOG_PID | syslog.LOG_CONS) parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display driver version') parser.add_option('--debug', dest='debug', action='store_true', help='display diagnostic information while running') parser.add_option("--iface", dest="iface", type=str, metavar="IFACE", help="specify the interface, e.g., u or /dev/ttyS0") parser.add_option('--sensors', dest='sensors', action='store_true', help='display list attached sensors') parser.add_option('--readings', dest='readings', action='store_true', help='display sensor readings') parser.add_option('--reading',dest='reading',type=str,metavar="SENSOR", help='display output of specified sensor') (options, args) = parser.parse_args() if options.version: print "owfs version %s" % DRIVER_VERSION exit(1) # default to usb for the interface iface = options.iface if options.iface is not None else 'u' if options.debug is not None: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) if options.sensors: ow.init(iface) traverse(ow.Sensor('/'), identify_sensor) elif options.readings: ow.init(iface) traverse(ow.Sensor('/'), display_sensor_info) elif options.reading: ow.init(iface) print '%s: %s' % (options.reading, ow.owfs_get(options.reading))
def main(): import sys, os, ConfigParser script_home = os.path.abspath(os.path.dirname(__file__)) parent = os.path.pardir sys.path.append(os.path.abspath(os.path.join(script_home, "../"))) import globalconfig globalconfig.load_global_config() config = globalconfig.get_global_config() syslog.openlog(config.get("notifier", "name"), logoption=syslog.LOG_PID) syslog.setlogmask(syslog.LOG_UPTO(getattr(syslog, config.get("notifier", "log_level")))) pid_file = config.get("notifier", 'pid_file') if not os.path.isabs(pid_file): # it's relative to our home directory pid_file = os.path.abspath(os.path.join(script_home, pid_file)) show_stats = config.getboolean("notifier", "show_stats") if show_stats: stats_file = config.get("notifier", "stats_file") if not os.path.isabs(stats_file): stats_file = os.path.abspath(os.path.join(script_home, stats_file)) else: stats_file = None notifier = Notifier( address = (config.get("notifier", 'wait_on_address'), config.getint("notifier", 'wait_on_port')), pidfile = pid_file, name = config.get("notifier", 'name'), foreground = config.getboolean("notifier", 'foreground'), listen_queue_len = config.getint("notifier", 'listen_queue_len'), show_stats = show_stats, stats_file = stats_file ) notifier.do_from_arg(sys.argv[1] if len(sys.argv) == 2 else None)
def setLogLevel( level, isDaemon): option = syslog.LOG_PID if not isDaemon: option |= syslog.LOG_PERROR syslog.openlog( str(QApplication.applicationName()), option, syslog.LOG_USER) atexit.register(Application.cleanup) if level == 0: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_WARNING)) elif level == 1: syslog.setlogmask(syslog.LOG_UPTO( syslog.LOG_NOTICE)) elif level == 2: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) syslog.syslog( syslog.LOG_INFO, "INFO logging to syslog")
def setup_logging(_initialized=[False]): """Initialize logging infrastructure.""" # Takes no arguments; see http://effbot.org/zone/default-values.htm if _initialized[0]: syslog.closelog() opts = syslog.LOG_PID | syslog.LOG_NDELAY facility = syslog.LOG_LOCAL0 if _args.facility: fac_name = ''.join(('LOG_', _args.facility.upper())) # We can assert this because we limit choices to _facilities_choices. assert fac_name in dir(syslog) facility = syslog.__dict__[fac_name] syslog.openlog(_pgm[:8], opts, facility) if _args.verbosity == 0: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_NOTICE)) if _args.verbosity == 1: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) elif _args.verbosity > 1: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) _initialized[0] = True return None
def loglevel(level): global _oldloglevel _oldloglevel = syslog.setlogmask(syslog.LOG_UPTO(level))
def logmask(self, newmask): syslog.setlogmask(newmask)
def main(options, args, EngineClass=StdEngine) : """Prepare the main loop and run it. Mostly consists of a bunch of high-level preparatory calls, protected by try blocks in the case of an exception.""" # Set the logging facility. syslog.openlog(options.log_label, syslog.LOG_PID | syslog.LOG_CONS) # Set up the signal handlers. signal.signal(signal.SIGHUP, sigHUPhandler) signal.signal(signal.SIGTERM, sigTERMhandler) syslog.syslog(syslog.LOG_INFO, "engine: Initializing weewx version %s" % weewx.__version__) syslog.syslog(syslog.LOG_INFO, "engine: Using Python %s" % sys.version) syslog.syslog(syslog.LOG_INFO, "engine: Platform %s" % platform.platform()) # Save the current working directory. A service might # change it. In case of a restart, we need to change it back. cwd = os.getcwd() if options.daemon: syslog.syslog(syslog.LOG_INFO, "engine: pid file is %s" % options.pidfile) daemon.daemonize(pidfile=options.pidfile) # be sure that the system has a reasonable time (at least 1 jan 2000). # log any problems every minute. ts = time.time() n = 0 while ts < 946684800: if n % 120 == 0: syslog.syslog(syslog.LOG_INFO, "engine: waiting for sane time. current time is %s" % weeutil.weeutil.timestamp_to_string(ts)) n += 1 time.sleep(0.5) ts = time.time() while True: os.chdir(cwd) config_path = os.path.abspath(args[0]) config_dict = getConfiguration(config_path) # Look for the debug flag. If set, ask for extra logging weewx.debug = int(config_dict.get('debug', 0)) if weewx.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) try: syslog.syslog(syslog.LOG_DEBUG, "engine: Initializing engine") # Create and initialize the engine engine = EngineClass(config_dict) syslog.syslog(syslog.LOG_INFO, "engine: Starting up weewx version %s" % weewx.__version__) # Start the engine. It should run forever unless an exception occurs. Log it # if the function returns. engine.run() syslog.syslog(syslog.LOG_CRIT, "engine: Unexpected exit from main loop. Program exiting.") # Catch any console initialization error: except InitializationError, e: # Log it: syslog.syslog(syslog.LOG_CRIT, "engine: Unable to load driver: %s" % e) # See if we should loop, waiting for the console to be ready, or exit: if options.loop_on_init: syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") else: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) # Catch any recoverable weewx I/O errors: except weewx.WeeWxIOError, e: # Caught an I/O error. Log it, wait 60 seconds, then try again syslog.syslog(syslog.LOG_CRIT, "engine: Caught WeeWxIOError: %s" % e) if options.exit: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...")
def logit(lvl, msg): syslog.openlog(name) syslog.setlogmask(syslog.LOG_UPTO(min_lvl)) syslog.syslog(''.join(('(', LEVELS[lvl], ') ', msg))) syslog.closelog()
def main(): global must_run args = _syntax().parse_args() # Define signal handler signal.signal(signal.SIGTERM, sig_handler) signal.signal(signal.SIGINT, sig_handler) signal.signal(signal.SIGHUP, sig_handler) # Initiate logging syslog.openlog(ident='vio4wd_core_scheduler', logoption=syslog.LOG_PID, facility=syslog.LOG_USER) loglevel = args.loglevel if args.loglevel else 7 if loglevel > 7 or loglevel < 0: loglevel = 7 syslog.syslog( syslog.LOG_WARNING, "Specified invalid loglevel. Defaulting to %d" % loglevel) syslog.setlogmask(syslog.LOG_UPTO(loglevel)) syslog.syslog(syslog.LOG_NOTICE, "Dynamic load balancing initiated...") if args.sensitivity < 0: args.sensitivity = 0.25 syslog.syslog( syslog.LOG_WARNING, "Specified invalid sensitivity. Defaulting to %f" % args.sensitivity) poll_interval = 5. if args.poll_interval: poll_interval = args.poll_interval syslog.syslog(syslog.LOG_INFO, "Polling every %.2f seconds." % poll_interval) # Initialize stats client stats_ep = args.stats_ep if args.stats_ep else "ipc:///var/run/virtio-forwarder/stats" syslog.syslog(syslog.LOG_INFO, "Connecting to stats server on %s..." % stats_ep) stats_sock = open_socket(stats_ep) # Get stats for initial worker core mapping stats_request = relay_pb2.StatsRequest() stats_request.include_inactive = False stats_request.delay = 200 stats_response = relay_pb2.StatsResponse() stats_sock = reconnect_send_recv(stats_sock, 'stats', stats_request, stats_response, stats_ep, poll_interval)[1] # Initialize core scheduler client sched_ep = args.sched_ep if args.sched_ep else "ipc:///var/run/virtio-forwarder/core_sched" syslog.syslog(syslog.LOG_INFO, "Connecting to core_scheduler server on %s..." % sched_ep) sched_sock = open_socket(sched_ep) # Get worker core mapping sched_request = relay_pb2.CoreSchedRequest( op=relay_pb2.CoreSchedRequest.GET_EAL_CORES) sched_response = relay_pb2.CoreSchedResponse() sched_sock = reconnect_send_recv(sched_sock, 'core scheduler', sched_request, sched_response, sched_ep, poll_interval)[1] worker_cores = sched_response.eal_cores syslog.syslog(syslog.LOG_DEBUG, "Worker cores at startup:") print_core_mapping(stats_response, worker_cores, syslog.LOG_DEBUG) # Gather NUMA information node_cpus = {} for i in range(get_max_node() + 1): cpus = get_node_cpus(i) workers_on_numa = cpus & set(worker_cores) if len(workers_on_numa) > 0: node_cpus[i] = cpus.copy() # [ main processing loop while must_run: # Get stats relays = [] stats_response = relay_pb2.StatsResponse() err, stats_sock = reconnect_send_recv(stats_sock, 'stats', stats_request, stats_response, stats_ep, poll_interval) relays = stats_response.relay # Gather worker cores again if the server went down. if err: sched_response = relay_pb2.CoreSchedResponse() sched_sock = reconnect_send_recv(sched_sock, 'core scheduler', sched_request, sched_response, sched_ep, poll_interval)[1] worker_cores = sched_response.eal_cores syslog.syslog(syslog.LOG_DEBUG, "Worker cores upon reconnection:") print_core_mapping(stats_response, worker_cores, syslog.LOG_DEBUG) if len(relays) == 0: time.sleep(poll_interval) continue # There are running relays # Initialize rate and current mapping structures per NUMA node mapping_changed = False numa_info = {} for node, cpus in node_cpus.iteritems(): numa_info[node] = (list(cpus & set(worker_cores)), {}, []) for relay in relays: # Rates rate = RelayRate(relay) rate.estimate_vm2vf_load() rate.estimate_vf2vm_load() try: numa_info[relay.socket_id][1][relay.id] = rate except KeyError: syslog.syslog( syslog.LOG_WARNING, "%d is not a valid socket id! Relay %d will not form part of the optimization." % (relay.socket_id, relay.id)) continue # Current mapping req = relay_pb2.CoreSchedRequest.RelayCPU() req.relay_number = relay.id req.virtio2vf_cpu = relay.cpu.vm_to_vf req.vf2virtio_cpu = relay.cpu.vf_to_vm numa_info[relay.socket_id][2].append(req) # Merge NUMA infos if global optimization was requested if args.global_numa_opt: e0 = list(set(worker_cores)) e1 = {} e2 = [] for numa_node, info in numa_info.iteritems(): e1.update(info[1]) e2 = e2 + info[2] numa_info = {-1: (e0, e1, e2)} # [ [NUMA-local] optimization for numa_node, info in numa_info.iteritems(): workers = info[0] # set of worker cores relay_rates = info[1] # dict of relay rate objects current_mappings = info[ 2] # list of core mappings (CoreSchedRequest objects) if len(relay_rates) == 0: continue # Distribute loads # Use simple round robin algorithm: Optimal optimization would entail # integer programming which may be intractable when there are many # workers and/or relays. new_mappings, fval = round_robin(relay_rates, workers) # Check if a reshuffle is warranted: # We use coefficient of variation since it is unitless. # If the new mapping results in loads 'tighter' by some margin, # apply the new mapping, else do nothing. Parameter might require tuning. cv = fval # coefficient of variation prev_cv = get_variation_coefficient(current_mappings, relay_rates, workers) if cv == -1: # No loads are running. Do nothing. pass elif (cv + args.sensitivity) < prev_cv: syslog.syslog(syslog.LOG_INFO, "Migrating workers on NUMA %d..." % numa_node) # Trigger cpu migration sched_req = relay_pb2.CoreSchedRequest( op=relay_pb2.CoreSchedRequest.UPDATE, relay_cpu_map=new_mappings) sched_sock.send(sched_req.SerializePartialToString()) # Gather response # Do not attempt infinite reconnect here. sched_response = relay_pb2.CoreSchedResponse() try: sched_response.ParseFromString(sched_sock.recv()) if sched_response.status == relay_pb2.CoreSchedResponse.OK: syslog.syslog(syslog.LOG_INFO, "Scheduler response: OK") mapping_changed = True else: syslog.syslog(syslog.LOG_ERR, "Scheduler response: ERROR") except zmq.Again: syslog.syslog( syslog.LOG_ERROR, "Connection to server lost. " "Could not migrate workers on NUMA %d." % numa_node) else: syslog.syslog(syslog.LOG_INFO, "Worker cores still sufficiently balanced.") # ] end [NUMA-local] optimization if mapping_changed: # Print new mapping stats_sock.send(stats_request.SerializePartialToString()) stats_response = relay_pb2.StatsResponse() stats_response.ParseFromString(stats_sock.recv()) syslog.syslog(syslog.LOG_DEBUG, "New worker core mapping:") print_core_mapping(stats_response, worker_cores, syslog.LOG_DEBUG) time.sleep(poll_interval) # ] End while true syslog.syslog(syslog.LOG_NOTICE, "Stopping vio4wd_core_scheduler")
ipblocks = [ '--ipblock=iptables', '--ipblock=ip route', '--ipblock=/sbin/iptables', '--ipblock=/sbin/ip route', ] for ipblock in ipblocks: Log.Info(' Test --ipblock, subtest: %s - errors/warnings' % ipblock) args.append(ipblock) Log.Debug(' ... main(%s)' % (args)) value = self._run_main(args) # print 'got stdout and stderr', output.getvalue() self.assertFalse( TestBlockHosts.ERRORS_RE.search(value), 'main() output contains ERROR or WARNING:\n%s' % value) args.remove(ipblock) if __name__ == '__main__': syslog.openlog(SCRIPT_ID) # don't syslog anything (EMERG is 0, so can't use it, use ALERT) syslog.setlogmask(syslog.LOG_ALERT) unittest.main()
def __exit__(self, extype, exvalue, traceback): syslog.setlogmask(self._oldloglevel)
actually adding any data to an existing database. The author wrote this program for his own use and is making it available to others in the hopes that it will be a useful example but there are absolutely no guarantees that this is in any way a working program... Use at your own risk! """ import argparse import csv import time import weeutil import weewx import weewx.archive import syslog syslog.setlogmask(syslog.LOG_UPTO( syslog.LOG_NOTICE)) # prevent flooding syslog #fields = ['date', 'time', 'outTemp', 'outHumidity', 'dewpoint', 'windSpeed', 'windGust10', 'windDir', 'rainRate', 'dayRain', # 'barometer', 'totalRain', 'inTemp', 'inHumidity', 'windGust', 'windchill', 'heatindex', # 'UV', 'radiation', 'ET', 'annualET', 'apparentTemp', 'maxradiation','hoursSunshine', 'windDir2'] # csv file fields fields = ["dateTime","usUnits","interval","barometer","pressure","altimeter","inTemp","outTemp","inHumidity","outHumidity", \ "windSpeed","windDir","windGust","windGustDir","rainRate","rain","dewpoint","windchill","heatindex","ET","radiation","UV", \ "extraTemp1","extraTemp2","extraTemp3","soilTemp1","soilTemp2","soilTemp3","soilTemp4","leafTemp1","leafTemp2","extraHumid1","extraHumid2", \ "soilMoist1","soilMoist2","soilMoist3","soilMoist4","leafWet1","leafWet2","rxCheckPercent","txBatteryStatus","consBatteryVoltage", \ "hail","hailRate","heatingTemp","heatingVoltage","supplyVoltage","referenceVoltage","windBatteryStatus","rainBatteryStatus", \ "outTempBatteryStatus","inTempBatteryStatus", \ "cpuLoadFactor","cpuUsagePercent","cpuWaitPercent","cpuTemperature", \ "logErrors","logIOErrors","logOPErrors","logFTPErrors","logRFErrors" ] # Create a command line parser:
def main(options, args, engine_class=StdEngine): """Prepare the main loop and run it. Mostly consists of a bunch of high-level preparatory calls, protected by try blocks in the case of an exception.""" # Set the logging facility. syslog.openlog(options.log_label, syslog.LOG_PID | syslog.LOG_CONS) # Set up the signal handlers. signal.signal(signal.SIGHUP, sigHUPhandler) signal.signal(signal.SIGTERM, sigTERMhandler) syslog.syslog(syslog.LOG_INFO, "engine: Initializing weewx version %s" % weewx.__version__) syslog.syslog(syslog.LOG_INFO, "engine: Using Python %s" % sys.version) syslog.syslog(syslog.LOG_INFO, "engine: Platform %s" % platform.platform()) # Save the current working directory. A service might # change it. In case of a restart, we need to change it back. cwd = os.getcwd() if options.daemon: syslog.syslog(syslog.LOG_INFO, "engine: pid file is %s" % options.pidfile) daemon.daemonize(pidfile=options.pidfile) # for backward compatibility, recognize loop_on_init from command-line loop_on_init = options.loop_on_init # be sure that the system has a reasonable time (at least 1 jan 2000). # log any problems every minute. n = 0 while weewx.launchtime_ts < 946684800: if n % 120 == 0: syslog.syslog(syslog.LOG_INFO, "engine: waiting for sane time. current time is %s" % weeutil.weeutil.timestamp_to_string(weewx.launchtime_ts)) n += 1 time.sleep(0.5) weewx.launchtime_ts = time.time() while True: os.chdir(cwd) config_path = os.path.abspath(args[0]) config_dict = getConfiguration(config_path) # Look for the debug flag. If set, ask for extra logging weewx.debug = int(config_dict.get('debug', 0)) if weewx.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) # See if there is a loop_on_init directive in the configuration, but # use it only if nothing was specified via command-line. if loop_on_init is None: loop_on_init = to_bool(config_dict.get('loop_on_init', False)) try: syslog.syslog(syslog.LOG_DEBUG, "engine: Initializing engine") # Create and initialize the engine engine = engine_class(config_dict) syslog.syslog(syslog.LOG_INFO, "engine: Starting up weewx version %s" % weewx.__version__) # Start the engine. It should run forever unless an exception # occurs. Log it if the function returns. engine.run() syslog.syslog(syslog.LOG_CRIT, "engine: Unexpected exit from main loop. Program exiting.") # Catch any console initialization error: except InitializationError, e: # Log it: syslog.syslog(syslog.LOG_CRIT, "engine: Unable to load driver: %s" % e) # See if we should loop, waiting for the console to be ready. # Otherwise, just exit. if loop_on_init: syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") else: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) # Catch any recoverable weewx I/O errors: except weewx.WeeWxIOError, e: # Caught an I/O error. Log it, wait 60 seconds, then try again syslog.syslog(syslog.LOG_CRIT, "engine: Caught WeeWxIOError: %s" % e) if options.exit: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...")
import syslog # Read configuration from /etc/pam_mkhomedir.ini config = ConfigParser.ConfigParser() config.read("/etc/pam_mkhomedir.ini") home_dir = config.get("config", "home_dir") scratch_dir = config.get("config", "scratch_dir") skel_dir = config.get("config", "skel_dir") debug_level = config.get("config", "debug_level") acl = config.get("config", "acl") syslog.openlog("pam_mkhomedir", syslog.LOG_PID, syslog.LOG_AUTH) if debug_level == "error": syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) elif debug_level == "debug": syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: # info or something wrong syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) def debug(msg): syslog.syslog(syslog.LOG_DEBUG, msg) def error(msg): syslog.syslog(syslog.LOG_ERR, msg) def info(msg):
def get_logmask(): return syslog.setlogmask(0)
usageThisMonth = sum(sum(x) for x in myDB['data'][currentYear][currentMonth].values()) if usageThisMonth > dailyCap: syslog.syslog(syslog.LOG_NOTICE,"Over daily cap") return True if usageThisMonth > monthlyCap: syslog.syslog(syslog.LOG_NOTICE,"Over monthly cap") return True syslog.syslog(syslog.LOG_INFO,"Daily cap remaining:%s Monthly cap remaining:%s " % (str(float(dailyCap-usageThisMonth)/1073741824),str(float(monthlyCap-usageThisMonth)/1073741824))) return False if __name__ == '__main__': # set up logging lvl = syslog.LOG_DEBUG #~ valid values are LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG. syslog.setlogmask(syslog.LOG_UPTO(lvl)) #~ change to control what is written to log file # set up database myDB = SetupDB() try: # sign in to our transmission client so we can get information and issue commands tc = transmissionrpc.Client(server,port=s_port,user=s_user,password=s_pass) # take a snapshot s_stats = tc.session_stats() # we won't have 288 entries per day in the database this way #~ if we want all 288 entires, move UpdateUsage outside of the if statement if s_stats.activeTorrentCount > 0: #if status == downloading, reannounce # TODO fix this for i in tc.get_torrents():
def loglevel_restore(): syslog.setlogmask(_oldloglevel)
def configDatabase(config_dict, binding, amplitude=1.0, day_phase_offset=0.0, annual_phase_offset=0.0, weather_phase_offset=0.0): """Configures the archive databases.""" global schema # Check to see if it already exists and is configured correctly. try: with weewx.manager.open_manager_with_config(config_dict, binding) as archive: if archive.firstGoodStamp() == start_ts and archive.lastGoodStamp( ) == stop_ts: # Database already exists. We're done. return except weedb.DatabaseError: pass # Delete anything that might already be there. try: weewx.manager.drop_database_with_config(config_dict, binding) except weedb.DatabaseError: pass # Need to build a new synthetic database. General strategy is to create the # archive data, THEN backfill with the daily summaries. This is faster than # creating the daily summaries on the fly. # First, we need to modify the configuration dictionary that was passed in # so it uses the DBManager, instead of the daily summary manager monkey_dict = config_dict.dict() monkey_dict['DataBindings'][binding]['manager'] = 'weewx.manager.Manager' with weewx.manager.open_manager_with_config(monkey_dict, binding, initialize=True) as archive: # Because this can generate voluminous log information, # suppress all but the essentials: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) # Now generate and add the fake records to populate the database: t1 = time.time() archive.addRecord( genFakeRecords(amplitude=amplitude, day_phase_offset=day_phase_offset, annual_phase_offset=annual_phase_offset, weather_phase_offset=weather_phase_offset)) t2 = time.time() print "\nTime to create synthetic archive database = %6.2fs" % (t2 - t1, ) with weewx.manager.open_manager_with_config(config_dict, binding, initialize=True) as archive: # Now go back to regular logging: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Backfill with daily summaries: t1 = time.time() nrecs, ndays = archive.backfill_day_summary() tdiff = time.time() - t1 if nrecs: print "\nProcessed %d records to backfill %d day summaries in %.2f seconds" % ( nrecs, ndays, tdiff) else: print "Daily summaries up to date."
def __enter__(self): self._oldloglevel = syslog.setlogmask(syslog.LOG_UPTO(self._level))
self.process_zeromq_message() else: self.run_tick() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--debug', help='Enable debug output', action='store_true') args = parser.parse_args() syslog.openlog('card', syslog.LOG_PID, syslog.LOG_DAEMON) if not args.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) system = System() dispatcher = Dispatcher(system) time.sleep(0.2) # wait for the ZeroMQ socket to establish a connection dispatcher.boot() card = Card(dispatcher) card.setup_zeromq() try: card.run() except KeyboardInterrupt: syslog.syslog(syslog.LOG_NOTICE, "Exiting program.")
def main(options, args, EngineClass=StdEngine): """Prepare the main loop and run it. Mostly consists of a bunch of high-level preparatory calls, protected by try blocks in the case of an exception.""" # Set the logging facility. syslog.openlog('weewx', syslog.LOG_PID | syslog.LOG_CONS) # Set up the signal handlers. signal.signal(signal.SIGHUP, sigHUPhandler) signal.signal(signal.SIGTERM, sigTERMhandler) syslog.syslog(syslog.LOG_INFO, "engine: Initializing weewx version %s" % weewx.__version__) syslog.syslog(syslog.LOG_INFO, "engine: Using Python %s" % sys.version) syslog.syslog(syslog.LOG_INFO, "engine: Platform %s" % platform.platform()) # Save the current working directory. A service might # change it. In case of a restart, we need to change it back. cwd = os.getcwd() if options.daemon: syslog.syslog(syslog.LOG_INFO, "engine: pid file is %s" % options.pidfile) daemon.daemonize(pidfile=options.pidfile) while True: os.chdir(cwd) config_path = os.path.abspath(args[0]) config_dict = getConfiguration(config_path) # Look for the debug flag. If set, ask for extra logging weewx.debug = int(config_dict.get('debug', 0)) if weewx.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) try: syslog.syslog(syslog.LOG_DEBUG, "engine: Initializing engine") # Create and initialize the engine engine = EngineClass(config_dict) syslog.syslog( syslog.LOG_INFO, "engine: Starting up weewx version %s" % weewx.__version__) # Start the engine engine.run() # Catch any console initialization error: except InitializationError, e: # Log it: syslog.syslog(syslog.LOG_CRIT, "engine: Unable to load driver: %s" % e) # See if we should loop, waiting for the console to be ready, or exit: if options.loop_on_init: syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") else: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) # Catch any recoverable weewx I/O errors: except weewx.WeeWxIOError, e: # Caught an I/O error. Log it, wait 60 seconds, then try again syslog.syslog(syslog.LOG_CRIT, "engine: Caught WeeWxIOError: %s" % e) if options.exit: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...")
actually adding any data to an existing database. The author wrote this program for his own use and is making it available to others in the hopes that it will be a useful example but there are absolutely no guarantees that this is in any way a working program... Use at your own risk! """ import argparse import csv import time import weeutil import weewx import weewx.archive import syslog syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_NOTICE)) # prevent flooding syslog #fields = ['date', 'time', 'outTemp', 'outHumidity', 'dewpoint', 'windSpeed', 'windGust10', 'windDir', 'rainRate', 'dayRain', # 'barometer', 'totalRain', 'inTemp', 'inHumidity', 'windGust', 'windchill', 'heatindex', # 'UV', 'radiation', 'ET', 'annualET', 'apparentTemp', 'maxradiation','hoursSunshine', 'windDir2'] # csv file fields fields = ["dateTime","usUnits","interval","barometer","pressure","altimeter","inTemp","outTemp","inHumidity","outHumidity", \ "windSpeed","windDir","windGust","windGustDir","rainRate","rain","dewpoint","windchill","heatindex","ET","radiation","UV", \ "extraTemp1","extraTemp2","extraTemp3","soilTemp1","soilTemp2","soilTemp3","soilTemp4","leafTemp1","leafTemp2","extraHumid1","extraHumid2", \ "soilMoist1","soilMoist2","soilMoist3","soilMoist4","leafWet1","leafWet2","rxCheckPercent","txBatteryStatus","consBatteryVoltage", \ "hail","hailRate","heatingTemp","heatingVoltage","supplyVoltage","referenceVoltage","windBatteryStatus","rainBatteryStatus", \ "outTempBatteryStatus","inTempBatteryStatus", \ "cpuLoadFactor","cpuUsagePercent","cpuWaitPercent","cpuTemperature", \ "logErrors","logIOErrors","logOPErrors","logFTPErrors","logRFErrors" ] # Create a command line parser:
, 'sambaPwdLastSet' : ('always', datecvt, 'samba_pwd_last_set') , 'sambaPwdMustChange' : ('always', datecvt, 'samba_pwd_must_change') , 'shadowExpire' : ('always', datecvt, 'shadow_expire') , 'shadowFlag' : ('always', bool, 'shadow_used') , 'shadowInactive' : ('always', int, 'shadow_inactive') , 'shadowLastChange' : ('always', datecvt, 'shadow_last_change') , 'shadowMax' : ('always', int, 'shadow_max') , 'shadowMin' : ('always', int, 'shadow_min') , 'shadowWarning' : ('always', int, 'shadow_warning') , 'uidNumber' : ('once', int, 'uid') , 'userPassword' : ('always', str, 'user_password') } config = Config() openlog(config.LOG_PREFIX, 0, config.LOG_FACILITY) setlogmask(LOG_UPTO(config.LOGLEVEL)) syslog(LOG_DEBUG, "started") try: tracker = instance.open(config.TRACKER) db = tracker.open(config.ROUNDUP_USER) ld = ldap.initialize(config.URL) ld.simple_bind_s(config.BIND_DN, config.BIND_PW) except StandardError, cause: log_traceback(cause) sys.exit(23) for line in sys.stdin: name = line.strip() syslog(LOG_DEBUG, "name=%s" % name) try: u = db.user.getnode(db.user.lookup(name))
def start(name, log_level): syslog.openlog(name) syslog.setlogmask(syslog.LOG_UPTO(LOG_MAP[log_level])) logging.config.dictConfig(LOGGER_CONF)
def main(): import optparse import weecfg import weewx def get_ids(stn_dict): """Display BloomSky device IDs associated with an API key.""" # get a BloomskyDriver object driver = BloomskyDriver(**stn_dict) ids = driver.ids if len(ids) > 1: print("Found BloomSky device IDs: %s" % ', '.join(ids)) elif len(ids) == 1: print("Found BloomSky device ID: %s" % ', '.join(ids)) else: print("No BloomSky device IDS found") driver.closePort() exit(0) def test_driver(stn_dict): """Test the BloomSky driver.""" # wrap in a try..except so we can pickup a keyboard interrupt try: # get a BloomskyDriver object driver = BloomskyDriver(**stn_dict) # continuously get loop packets and print them to screen for pkt in driver.genLoopPackets(): print(weeutil.weeutil.timestamp_to_string(pkt['dateTime']), pkt) except KeyboardInterrupt: # we have a keyboard interrupt so shut down driver.closePort() def get_json_data(stn_dict): """Obtain BloomSky API response and display in JSON format.""" # extract the API key api_key = stn_dict.get('api_key') # if we have an API key then get the data otherwise exit after # informing the user about the problem if api_key: # get an ApiClient object api_client = ApiClient(api_key=api_key) # get the JSON response raw_data = api_client.sd.get_data() # do we have any raw data? if raw_data is not None: # yes, display the JSON response on screen print(json.dumps(raw_data, sort_keys=True, indent=2)) else: # no, display an appropriate message print("Unable to obtain JSON data") exit(0) else: print("BloomSky API key required.") print("Specify API key in configuration file under [Bloomsky] or use --api_key option.") print("Exiting.") exit(1) usage = """Usage: python -m user.bloomsky --help python -m user.bloomsky --version python -m user.bloomsky --test-driver [CONFIG_FILE|--config=CONFIG_FILE] [--api-key=API_KEY] [--debug=0|1|2|3] python -m user.bloomsky --get-json-data [CONFIG_FILE|--config=CONFIG_FILE] [--api-key=API_KEY] [--debug=0|1|2|3] python -m user.bloomsky --get-device-ids [CONFIG_FILE|--config=CONFIG_FILE] [--api-key=API_KEY] [--debug=0|1|2|3]""" parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display BloomSky driver version number') parser.add_option('--config', dest='config_path', metavar='CONFIG_FILE', help="use configuration file CONFIG_FILE.") parser.add_option('--debug', dest='debug', metavar='DEBUG', help='use WeeWX debug level DEBUG') parser.add_option('--test-driver', dest='test_driver', action='store_true', metavar='TEST_DRIVER', help='test the BloomSky driver') parser.add_option('--api-key', dest='api_key', metavar='API_KEY', help='BloomSky API key') parser.add_option('--get-json-data', dest='jdata', action='store_true', help='get BloomSky API json response') parser.add_option('--get-device-ids', dest='get_ids', action='store_true', help='get BloomSky device IDs associated with an API key') (opts, args) = parser.parse_args() # get config_dict to use config_path, config_dict = weecfg.read_config(opts.config_path, args) print("Using configuration file %s" % config_path) stn_dict = config_dict.get('Bloomsky', {}) # set weewx.debug as necessary if opts.debug is not None: _debug = weeutil.weeutil.to_int(opts.debug) else: _debug = weeutil.weeutil.to_int(config_dict.get('debug', 0)) weewx.debug = _debug # Now we can set up the user customized logging but we need to handle both # v3 and v4 logging. V4 logging is very easy but v3 logging requires us to # set up syslog and raise our log level based on weewx.debug try: # assume v 4 logging weeutil.logger.setup('weewx', config_dict) except AttributeError: # must be v3 logging, so first set the defaults for the system logger syslog.openlog('weewx', syslog.LOG_PID | syslog.LOG_CONS) # now raise the log level if required if weewx.debug > 0: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # display driver version number if opts.version: print("%s driver version: %s" % (DRIVER_NAME, DRIVER_VERSION)) exit(0) # do we have a specific API key to use if opts.api_key: stn_dict['api_key'] = opts.api_key obfuscated = ''.join(('"....', opts.api_key[-4:], '"')) print("Using BloomSky API key %s" % obfuscated) # display device IDs if opts.get_ids: get_ids(stn_dict) exit(0) # run the driver if opts.test_driver: test_driver(stn_dict) exit(0) # get BloomSky API JSON response if opts.jdata: get_json_data(stn_dict) exit(0) # otherwise print our help parser.print_help()
def set_loglevel(self, newlevel): l = self._int_to_level(newlevel) self.mask = syslog.LOG_UPTO(l) self.loglevel = newlevel syslog.setlogmask(self.mask)
def test_setlogmask(self): syslog.setlogmask(syslog.LOG_DEBUG)
cf = basicconfig.get_config("logging.conf") except basicconfig.ConfigReadError as err: warn(err, "Using default values.") FACILITY = "USER" LEVEL = "WARNING" else: FACILITY = cf.FACILITY LEVEL = cf.LEVEL del cf del basicconfig syslog.openlog(sys.argv[0].split("/")[-1], syslog.LOG_PID, getattr(syslog, "LOG_" + FACILITY)) _oldloglevel = syslog.setlogmask( syslog.LOG_UPTO(getattr(syslog, "LOG_" + LEVEL))) def close(): syslog.closelog() def debug(msg): syslog.syslog(syslog.LOG_DEBUG, _encode(msg)) def info(msg): syslog.syslog(syslog.LOG_INFO, _encode(msg)) def notice(msg):
if usageThisMonth > monthlyCap: syslog.syslog(syslog.LOG_NOTICE, "Over monthly cap") return True syslog.syslog( syslog.LOG_INFO, "Daily cap remaining:%s Monthly cap remaining:%s " % (str(float(dailyCap - usageThisMonth) / 1073741824), str(float(monthlyCap - usageThisMonth) / 1073741824))) return False if __name__ == '__main__': # set up logging lvl = syslog.LOG_DEBUG #~ valid values are LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG. syslog.setlogmask( syslog.LOG_UPTO(lvl)) #~ change to control what is written to log file # set up database myDB = SetupDB() try: # sign in to our transmission client so we can get information and issue commands tc = transmissionrpc.Client(server, port=s_port, user=s_user, password=s_pass) # take a snapshot s_stats = tc.session_stats() # we won't have 288 entries per day in the database this way #~ if we want all 288 entires, move UpdateUsage outside of the if statement if s_stats.activeTorrentCount > 0: #if status == downloading, reannounce
def logmask(self): return syslog.setlogmask(0)
def main(options, args, engine_class=StdEngine): """Prepare the main loop and run it. Mostly consists of a bunch of high-level preparatory calls, protected by try blocks in the case of an exception.""" # Set the logging facility. syslog.openlog(options.log_label, syslog.LOG_PID | syslog.LOG_CONS) # Set up the signal handlers. signal.signal(signal.SIGHUP, sigHUPhandler) signal.signal(signal.SIGTERM, sigTERMhandler) syslog.syslog(syslog.LOG_INFO, "engine: Initializing weewx version %s" % weewx.__version__) syslog.syslog(syslog.LOG_INFO, "engine: Using Python %s" % sys.version) syslog.syslog(syslog.LOG_INFO, "engine: Platform %s" % platform.platform()) syslog.syslog(syslog.LOG_INFO, "engine: Locale is '%s'" % locale.setlocale(locale.LC_ALL)) # Save the current working directory. A service might # change it. In case of a restart, we need to change it back. cwd = os.getcwd() # Get the path to the configuration file config_path = os.path.abspath(args[0]) if options.daemon: syslog.syslog(syslog.LOG_INFO, "engine: pid file is %s" % options.pidfile) daemon.daemonize(pidfile=options.pidfile) # For backward compatibility, recognize loop_on_init from command-line loop_on_init = options.loop_on_init # Make sure the system time is not out of date (a common problem with the Raspberry Pi). # Do this by making sure the system time is later than the creation time of the config file sane = os.stat(config_path).st_ctime n = 0 while weewx.launchtime_ts < sane: # Log any problems every minute. if n % 120 == 0: syslog.syslog( syslog.LOG_INFO, "engine: Waiting for sane time. Current time is %s" % weeutil.weeutil.timestamp_to_string(weewx.launchtime_ts)) n += 1 time.sleep(0.5) weewx.launchtime_ts = time.time() while True: os.chdir(cwd) config_dict = getConfiguration(config_path) # Look for the debug flag. If set, ask for extra logging weewx.debug = int(config_dict.get('debug', 0)) if weewx.debug: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) syslog.syslog(syslog.LOG_DEBUG, "engine: Debug is %s" % weewx.debug) # See if there is a loop_on_init directive in the configuration, but # use it only if nothing was specified via command-line. if loop_on_init is None: loop_on_init = to_bool(config_dict.get('loop_on_init', False)) try: syslog.syslog(syslog.LOG_DEBUG, "engine: Initializing engine") # Create and initialize the engine engine = engine_class(config_dict) syslog.syslog( syslog.LOG_INFO, "engine: Starting up weewx version %s" % weewx.__version__) # Start the engine. It should run forever unless an exception # occurs. Log it if the function returns. engine.run() syslog.syslog( syslog.LOG_CRIT, "engine: Unexpected exit from main loop. Program exiting.") # Catch any console initialization error: except InitializationError as e: # Log it: syslog.syslog(syslog.LOG_CRIT, "engine: Unable to load driver: %s" % e) # See if we should loop, waiting for the console to be ready. # Otherwise, just exit. if loop_on_init: syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") else: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) # Catch any recoverable weewx I/O errors: except weewx.WeeWxIOError as e: # Caught an I/O error. Log it, wait 60 seconds, then try again syslog.syslog(syslog.LOG_CRIT, "engine: Caught WeeWxIOError: %s" % e) if options.exit: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.IO_ERROR) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 60 seconds then retrying...") time.sleep(60) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") except (weedb.CannotConnect, weedb.DisconnectError) as e: # No connection to the database server. Log it, wait 120 seconds, then try again syslog.syslog(syslog.LOG_CRIT, "engine: Database connection exception: %s" % e) if options.exit: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.DB_ERROR) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 2 minutes then retrying...") time.sleep(120) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") except weedb.OperationalError as e: # Caught a database error. Log it, wait 120 seconds, then try again syslog.syslog( syslog.LOG_CRIT, "engine: Database OperationalError exception: %s" % e) if options.exit: syslog.syslog(syslog.LOG_CRIT, " **** Exiting...") sys.exit(weewx.DB_ERROR) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 2 minutes then retrying...") time.sleep(120) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") except OSError as e: # Caught an OS error. Log it, wait 10 seconds, then try again syslog.syslog(syslog.LOG_CRIT, "engine: Caught OSError: %s" % e) weeutil.weeutil.log_traceback(" **** ", syslog.LOG_DEBUG) syslog.syslog(syslog.LOG_CRIT, " **** Waiting 10 seconds then retrying...") time.sleep(10) syslog.syslog(syslog.LOG_NOTICE, "engine: retrying...") except Restart: syslog.syslog(syslog.LOG_NOTICE, "engine: Received signal HUP. Restarting.") except Terminate: syslog.syslog( syslog.LOG_INFO, "engine: Terminating weewx version %s" % weewx.__version__) weeutil.weeutil.log_traceback(" **** ", syslog.LOG_DEBUG) # Reraise the exception (this should cause the program to exit) raise # Catch any keyboard interrupts and log them except KeyboardInterrupt: syslog.syslog(syslog.LOG_CRIT, "engine: Keyboard interrupt.") # Reraise the exception (this should cause the program to exit) raise # Catch any non-recoverable errors. Log them, exit except Exception as ex: # Caught unrecoverable error. Log it, exit syslog.syslog(syslog.LOG_CRIT, "engine: Caught unrecoverable exception in engine:") syslog.syslog(syslog.LOG_CRIT, " **** %s" % ex) # Include a stack traceback in the log: weeutil.weeutil.log_traceback(" **** ", syslog.LOG_CRIT) syslog.syslog(syslog.LOG_CRIT, " **** Exiting.") # Reraise the exception (this should cause the program to exit) raise
def __init__(self, console=None): syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) self.console = console
port = self._prompt('port', '/dev/ttyUSB0') return {'port': port} # define a main entry point for basic testing of the station without weewx # engine and service overhead. invoke this as follows from the weewx root dir: # # PYTHONPATH=bin python bin/weewx/drivers/ultimeter.py if __name__ == '__main__': import optparse usage = """%prog [options] [--help]""" syslog.openlog('ultimeter', syslog.LOG_PID | syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display driver version') parser.add_option('--port', dest='port', metavar='PORT', help='serial port to which the station is connected', default=DEFAULT_PORT) (options, args) = parser.parse_args() if options.version: print "ultimeter driver version %s" % DRIVER_VERSION exit(0) with Station(options.port) as s: s.set_logger_mode() while True:
def main(): FMT_TE923TOOL = 'te923tool' FMT_DICT = 'dict' FMT_TABLE = 'table' syslog.openlog('wee_te923', syslog.LOG_PID | syslog.LOG_CONS) parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display driver version') parser.add_option('--debug', dest='debug', action='store_true', help='display diagnostic information while running') parser.add_option('--status', dest='status', action='store_true', help='display station status') parser.add_option('--readings', dest='readings', action='store_true', help='display sensor readings') parser.add_option("--records", dest="records", type=int, metavar="N", help="display N station records, oldest to newest") parser.add_option('--blocks', dest='blocks', type=int, metavar="N", help='display N 32-byte blocks of station memory') parser.add_option("--format", dest="format", type=str, metavar="FORMAT", help="format for output: te923tool, table, or dict") (options, _) = parser.parse_args() if options.version: print "te923 driver version %s" % DRIVER_VERSION exit(1) if options.debug is not None: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) else: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_INFO)) if options.format is None: options.format = FMT_TE923TOOL elif (options.format.lower() != FMT_TE923TOOL and options.format.lower() != FMT_TABLE and options.format.lower() != FMT_DICT): print "Unknown format '%s'. Known formats include 'te923tool', 'table', and 'dict'." % options.format exit(1) station = None try: station = TE923() station.open() if options.status: data = station.get_status() if options.format.lower() == FMT_DICT: print_dict(data) elif options.format.lower() == FMT_TABLE: print_table(data) else: print_status(data) if options.readings: data = station.get_readings() if options.format.lower() == FMT_DICT: print_dict(data) elif options.format.lower() == FMT_TABLE: print_table(data) else: print_readings(data) if options.records is not None: for ptr, data in station.gen_records(count=options.records): if options.format.lower() == FMT_DICT: print_dict(data) else: print_readings(data) if options.blocks is not None: for ptr, block in station.gen_blocks(count=options.blocks): print_hex(ptr, block) finally: if station is not None: station.close()
print("example /dev/rfcomm0 or /dev/serial0") port = self._prompt('port', '/dev/serial0') return {'port': port} # define a main entry point for basic testing of the station without weewx # engine and service overhead. invoke this as follows: # PYTHONPATH=/usr/share/weewx python3 bin/user/weatherradio.py if __name__ == '__main__': import optparse usage = """%prog [options] [--help]""" syslog.openlog('weatherradio', syslog.LOG_PID | syslog.LOG_CONS) syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) parser = optparse.OptionParser(usage=usage) parser.add_option('--version', dest='version', action='store_true', help='display driver version') parser.add_option('--port', dest='port', metavar='PORT', help='serial port to which the station is connected', default=DEFAULT_PORT) (options, args) = parser.parse_args() if options.version: print("weatherradio driver version %s" % DRIVER_VERSION) exit(0) with Station(options.port) as s: while True: print(time.time(), Station.parse_readings(s.get_readings()))
# load gateway config # with open(gateway_config) as gwfile: for line in gwfile: if not line.strip().startswith('#'): name, val = line.partition("=")[::2] gw_config[name.strip()] = val.strip() gwfile.close() # # setup syslog # fac = eval('syslog.LOG_' + gw_config['LOGFACILITY'].upper()) mask = eval('syslog.LOG_' + gw_config['LOGMASK'].upper()) syslog.openlog(logoption=syslog.LOG_PID, facility=fac) syslog.setlogmask(syslog.LOG_UPTO(mask)) # # check python version # python_version=platform.python_version() if LooseVersion(python_version) >= LooseVersion(py_version_require): if options.DEBUG: syslog.syslog(syslog.LOG_DEBUG, 'Python Version Check: ' + str(python_version) + ' OK') else: syslog.syslog(syslog.LOG_ERR, 'Need more current Python version, require version: ' + str(py_version_require) + ' or newer') print('Exiting ...') syslog.closelog() sys.exit(1) #