fc@ s ddlmZddlZddlZddlZddlZ ddl Z ddl Z ejd<krzddlmZnddlmZmZmZmZmZmZmZmZddlmZmZmZddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)dd l*m+Z,ejd=krHdd lm-Z-ne.Z-d Z/d Z0d Z1dZ2dZ3ejd>krdZ4n ej5Z4i!e4d6e4d6e4d6e6d6e6d6e6d6e6d6e6d6e6d6e6d6e6d6e6d6e6d6e6d6e6d6e6d 6e6d!6e6d"6e6d#6e6d$6e6d%6e2d&6e1d'6e0d(6e/d)6e3d*6e7d+6e6d,6e6d-6e6d.6e6d/6e6d06e2d16Z8e9d2Z:d3Z;d4e fd5YZ<d6Z=d7Z>ddddd8Z@eeAd9ZBd:e jCfd;YZDdS(?i(tdivisionNi(tChainMap(t LOG_EMERGt LOG_ALERTtLOG_CRITtLOG_ERRt LOG_WARNINGt LOG_NOTICEtLOG_INFOt LOG_DEBUGi(t __version__tsendvt stream_fd( t_ReadertNOPtAPPENDt INVALIDATEt LOCAL_ONLYt RUNTIME_ONLYtSYSTEMt SYSTEM_ONLYt CURRENT_USERt _get_catalog(tid128(t MonotoniccC s0ttjd|dtjd|dfS(Nt microsecondsitbytesi(Rt _datetimet timedeltat_uuidtUUID(tm((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_convert_monotonic.scC stjdt|S(NR(RRtint(ts((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_convert_source_monotonic2scC stjj|dS(Ni@B(Rtdatetimet fromtimestamp(tt((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_convert_realtime5scC stjjt|dS(Ni@B(RR$R%R!(R"((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_convert_timestamp8scC s|S(N((tx((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_convert_trivial;scC stj|jS(N(RRtdecode(R"((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt _convert_uuid?st MESSAGE_IDt _MACHINE_IDt_BOOT_IDtPRIORITYtLEADERt SESSION_IDtUSERSPACE_USECt INITRD_USECt KERNEL_USECt_UIDt_GIDt_PIDtSYSLOG_FACILITYt SYSLOG_PIDt_AUDIT_SESSIONt_AUDIT_LOGINUIDt_SYSTEMD_SESSIONt_SYSTEMD_OWNER_UIDt CODE_LINEtERRNOt EXIT_STATUSt_SOURCE_REALTIME_TIMESTAMPt__REALTIME_TIMESTAMPt_SOURCE_MONOTONIC_TIMESTAMPt__MONOTONIC_TIMESTAMPt__CURSORtCOREDUMPt COREDUMP_PIDt COREDUMP_UIDt COREDUMP_GIDtCOREDUMP_SESSIONtCOREDUMP_SIGNALtCOREDUMP_TIMESTAMPtABCDEFGHIJKLMNOPQRTSUVWXYZ_cC st|t S(N(tsett _IDENT_LETTER(R"((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_valid_field_namejstReadercB seZdZdddddZdZdZdZdZe j dkr]eZ ndZ d d Z d d Zd Zdd ZdZddZdZdZddZddZRS(sUReader allows the access and filtering of systemd journal entries. Note that in order to access the system journal, a non-root user must be in the `systemd-journal` group. Example usage to print out all informational or higher level messages for systemd-udevd for this boot: >>> j = journal.Reader() >>> j.this_boot() >>> j.log_level(journal.LOG_INFO) >>> j.add_match(_SYSTEMD_UNIT="systemd-udevd.service") >>> for entry in j: ... print(entry['MESSAGE']) See systemd.journal-fields(7) for more info on typical fields found in the journal. icC stt|j|||tjdkrot|_|dk rY|jjj |n|jjj t n.t j |_|dk r|jj |ndS(sCreate an instance of Reader, which allows filtering and return of journal entries. Argument `flags` sets open flags of the journal, which can be one of, or ORed combination of constants: LOCAL_ONLY (default) opens journal on local machine only; RUNTIME_ONLY opens only volatile journal files; and SYSTEM_ONLY opens only journal files of system services and the kernel. Argument `path` is the directory of journal files. Note that `flags` and `path` are exclusive. Argument `converters` is a dictionary which updates the DEFAULT_CONVERTERS to convert journal field values. Field names are used as keys into this dictionary. The values must be single argument functions, which take a `bytes` object and return a converted value. When there's no entry for a field name, then the default UTF-8 decoding will be attempted. If the conversion fails with a ValueError, unconverted bytes object will be returned. (Note that ValueEror is a superclass of UnicodeDecodeError). Reader implements the context manager protocol: the journal will be closed when exiting the block. iN(ii( tsuperRRt__init__t_syst version_infot _ChainMapt converterstNonetmapstappendtDEFAULT_CONVERTERStcopytupdate(tselftflagstpathtfilesRX((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRTs   cC s?|jj|tj}y||SWntk r:|SXdS(s.Convert value using self.converters[key] If `key` is not present in self.converters, a standard unicode decoding will be attempted. If the conversion (either key-specific or the default one) fails with a ValueError, the original bytes object will be returned. N(RXtgetRR+t ValueError(R_tkeytvaluetconvert((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt_convert_fields  cC sxi}xk|jD]]\}}t|trZg|D]}|j||^q5||sN(RktextendRiRSRRt add_match(R_targstkwargstarg((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRxs  icC sztt|j|rstt|j}|rs|j|d<|j|d<|j|d<|j|SntS(sReturn the next log entry as a mapping type, currently a standard dictionary of fields. Optional skip value will return the `skip`\-th log entry. Entries will be processed with converters specified during Reader creation. RCRERF( RSRRt_nextt_get_allt _get_realtimet_get_monotonict _get_cursorRotdict(R_tskipRl((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRqs cC s|j| S(s5Return the previous log entry as a mapping type, currently a standard dictionary of fields. Optional skip value will return the -`skip`\-th log entry. Entries will be processed with converters specified during Reader creation. Equivalent to get_next(-skip). (Rq(R_R((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt get_previouss c s/tfdttjDS(sReturn unique values appearing in the journal for given `field`. Note this does not respect any journal matches. Entries will be processed with converters specified during Reader creation. c3 s!|]}j|VqdS(N(Rh(RvRf(tfieldR_(s5/usr/lib64/python2.7/site-packages/systemd/journal.pys s(RORSRRt query_unique(R_R((RR_s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRscC s8|dkrdn t|d}tt|j|S(s1Wait for a change in the journal. `timeout` is the maximum time in seconds to wait, or None, to wait forever. Returns one of NOP (no change), APPEND (new entries have been added to the end of the journal), or INVALIDATE (journal files have been added or removed). ii@BN(RYR!RSRRtwait(R_ttimeouttus((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRs"cC sJt|tjr.t|jdd}ntt|jt|S(sSeek to a matching journal entry nearest to `realtime` time. Argument `realtime` must be either an integer unix timestamp or datetime.datetime instance. s%s.%fi@B( RjRR$tfloattstrftimeRSRRt seek_realtimeR!(R_trealtime((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRscC sht|tjr!|j}nt|d}t|tjrO|j}ntt |j ||S(sGSeek to a matching journal entry nearest to `monotonic` time. Argument `monotonic` is a timestamp from boot in either seconds or a datetime.timedelta instance. Argument `bootid` is a string or UUID representing which boot the monotonic time is reference to. Defaults to current bootid. i@B( RjRRt totalsecondsR!RRthexRSRRtseek_monotonic(R_t monotonictbootid((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRs  cC s^d|kodknrNx;t|dD]}|jdd|q-Wn tddS(sASet maximum log `level` by setting matches for PRIORITY. iiiR0s%ds!Log level must be 0 <= level <= 7N(trangeRxRd(R_tlevelti((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt log_level+scC s2t|tjr|j}n|jd|dS(sAdd match for log entries with specified `messageid`. `messageid` can be string of hexadicimal digits or a UUID instance. Standard message IDs can be found in systemd.id128. Equivalent to add_match(MESSAGE_ID=`messageid`). R-N(RjRRRRx(R_t messageid((s5/usr/lib64/python2.7/site-packages/systemd/journal.pytmessageid_match4s cC sD|dkrtjj}nt|d|}|jd|dS(sAdd match for _BOOT_ID equal to current boot ID or the specified boot ID. If specified, bootid should be either a UUID or a 32 digit hex number. Equivalent to add_match(_BOOT_ID='bootid'). RR/N(RYt_id128tget_bootRtgetattrRx(R_R((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt this_boot@s cC sD|dkrtjj}nt|d|}|jd|dS(sAdd match for _MACHINE_ID equal to the ID of this machine. If specified, machineid should be either a UUID or a 32 digit hex number. Equivalent to add_match(_MACHINE_ID='machineid'). RR.N(RYRt get_machineRRRx(R_t machineid((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt this_machineMs N(i(t__name__t __module__t__doc__RYRTRhRoRpRtRURVtnextRxRqRRRRRRRRR(((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRRms&%       cC s(t|tjr|j}nt|S(N(RjRRRR(tmid((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyt get_catalog[s cC sUt|tr$|jdd|St|trE|dt|S|d|SdS(Nsutf-8t=(RjRtencodeR!tstr(RRf((s5/usr/lib64/python2.7/site-packages/systemd/journal.pyRu`s cK sd|g}|d k r?t|d|}|jd|n||koa|koad knrtjdddd \}}}n|d k r|jd|n|d k r|jd j|n|d k r|jd |n|jd |jDt|S( sySend a message to the journal. >>> journal.send('Hello world') >>> journal.send('Hello, again, world', FIELD2='Greetings!') >>> journal.send('Binary message', BINARY=b'\xde\xad\xbe\xef') Value of the MESSAGE argument will be used for the MESSAGE= field. MESSAGE must be a string and will be sent as UTF-8 to the journal. MESSAGE_ID can be given to uniquely identify the type of message. It must be a string or a uuid.UUID object. CODE_LINE, CODE_FILE, and CODE_FUNC can be specified to identify the caller. Unless at least on of the three is given, values are extracted from the stack frame of the caller of send(). CODE_FILE and CODE_FUNC must be strings, CODE_LINE must be an integer. Additional fields for the journal entry can only be specified as keyword arguments. The payload can be either a string or bytes. A string will be sent as UTF-8, and bytes will be sent as-is to the journal. Other useful fields include PRIORITY, SYSLOG_FACILITY, SYSLOG_IDENTIFIER, SYSLOG_PID. sMESSAGE=Rs MESSAGE_ID=tlimitiiis CODE_FILE=sCODE_LINE={:d}s CODE_FUNC=cs s$|]\}}t||VqdS(N(Ru(RvReRn((s5/usr/lib64/python2.7/site-packages/systemd/journal.pys sN( RYRR[t _tracebackt extract_stacktformatRwRiR (tMESSAGER-t CODE_FILER?t CODE_FUNCRzRytid((s5/usr/lib64/python2.7/site-packages/systemd/journal.pytsendhs  '&   cC s%t|||}tj|ddS(sNReturn a file object wrapping a stream to journal. Log messages written to this file as simple newline sepearted text strings are written to the journal. The file will be line buffered, so messages are actually sent after a newline character is written. >>> stream = journal.stream('myapp') >>> stream ', mode 'w' at 0x...> >>> stream.write('message...\n') will produce the following message in the journal:: PRIORITY=7 SYSLOG_IDENTIFIER=myapp MESSAGE=message... Using the interface with print might be more convinient: >>> from __future__ import print_function >>> print('message...', file=stream) priority is the syslog priority, one of `LOG_EMERG`, `LOG_ALERT`, `LOG_CRIT`, `LOG_ERR`, `LOG_WARNING`, `LOG_NOTICE`, `LOG_INFO`, `LOG_DEBUG`. level_prefix is a boolean. If true, kernel-style log priority level prefixes (such as '<1>') are interpreted. See sd-daemon(3) for more information. twi(R t_ostfdopen(t identifiertpriorityt level_prefixtfd((s5/usr/lib64/python2.7/site-packages/systemd/journal.pytstreams"tJournalHandlercB s5eZdZejdZdZedZRS(sJournal handler class for the Python logging framework. Please see the Python logging module documentation for an overview: http://docs.python.org/library/logging.html. To create a custom logger whose messages go only to journal: >>> log = logging.getLogger('custom_logger_name') >>> log.propagate = False >>> log.addHandler(journal.JournalHandler()) >>> log.warn("Some message: %s", detail) Note that by default, message levels `INFO` and `DEBUG` are ignored by the logging framework. To enable those log levels: >>> log.setLevel(logging.DEBUG) To redirect all logging messages to journal regardless of where they come from, attach it to the root logger: >>> logging.root.addHandler(journal.JournalHandler()) For more complex configurations when using `dictConfig` or `fileConfig`, specify `systemd.journal.JournalHandler` as the handler class. Only standard handler configuration options are supported: `level`, `formatter`, `filters`. To attach journal MESSAGE_ID, an extra field is supported: >>> import uuid >>> mid = uuid.UUID('0123456789ABCDEF0123456789ABCDEF') >>> log.warn("Message with ID", extra={'MESSAGE_ID': mid}) Fields to be attached to all messages sent through this handler can be specified as keyword arguments. This probably makes sense only for SYSLOG_IDENTIFIER and similar fields which are constant for the whole program: >>> journal.JournalHandler(SYSLOG_IDENTIFIER='my-cool-app') The following journal fields will be sent: `MESSAGE`, `PRIORITY`, `THREAD_NAME`, `CODE_FILE`, `CODE_LINE`, `CODE_FUNC`, `LOGGER` (name as supplied to getLogger call), `MESSAGE_ID` (optional, see above), `SYSLOG_IDENTIFIER` (defaults to sys.argv[0]). cK sstt|j|x-|D]%}t|std|qqWd|krftjd|ds      :F            1%