a =*f'2@s0dZdZdZddlZddlZddlmZddlmZm Z m Z dd l m Z e e Zed Zed d fZed ZedZedZedZedZedZedZedddfZedZedZedZedejZGdddeZ Gddde Z!Gdd d e Z"Gd!d"d"e Z#dS)#z Cyril Jaquierz Copyright (c) 2004 Cyril JaquierZGPLN)abstractmethod)reGroupDictStrptimetimeREgetTimePatternRE) getLoggerz(?/rz(^(?:\(\?\w+\))?(?:\^|\((?:\?:)?\^(?!\|))z(?r@ default_tzr r rgetDateszDateTemplate.getDatec Cs$tdtdtdtd|S)Nr )RE_EXEANC_BOUND_BEGr$RE_EXSANC_BOUND_BEGRE_EXLINE_BOUND_BEGRE_EXLINE_NO_BOUNDS)patternr r runboundPatterns zDateTemplate.unboundPattern)TT)NN)__name__ __module__ __qualname____doc__r*r.r)r,rrr6propertyr3r=rArrE staticmethodrKr r r rr9s"   @   rc@s$eZdZdZdddZd ddZdS) DateEpochzA date template which searches for Unix timestamps. This includes Unix timestamps which appear at start of a line, optionally within square braces (nsd), or on SELinux audit log lines. Attributes ---------- name regex FNcst||sdn||_||_d|_d|r>|s4dn||_d|r~tfdd|}t|sld|d }d |_| |n4|sd }|j |d d nd}|j |ddddS)NZEpochrz\d{10,11}\b(?:\.\d{3,6})?Z LongEpochz'\d{10,11}(?:\d{3}(?:\.\d{1,6}|\d{3})?)?csdS)Nz(%s)r )vZepochREr rrrz$DateEpoch.__init__..()rzf((?:^|(?P(?<=^\[))|(?P(?<=\baudit\()))%s)(?:(?(selinux)(?=:\d+\)))|(?(square)(?=\])))Fr4z*((?P(?<=^\[))?%s)(?(square)(?=\]))r T)r4r5) rrr_longFrm_grpIdxRE_EPOCH_PATTERNr$r'r#r6)rZ lineBeginOnlyrJZlongFrmr3r rTrrs&    zDateEpoch.__init__cCsn|s||}|rj||j}|jr^t|dkr^t|dkrRd|vrRt|d}n t|d}t||fSdS)axMethod to return the date for a log line. Parameters ---------- line : str Log line, of which the date should be extracted from. default_tz: ignored, Unix timestamps are time zone independent Returns ------- (float, str) Tuple containing a Unix timestamp, and the string of the date which was matched and in turned used to calculated the timestamp. .i@BiN)rAr rYrXlenfloat)rr>r@rDrSr r rrEs   zDateEpoch.getDate)FNF)NNrLrMrNrOrrEr r r rrRs rRcsheZdZdZe\ZZeeZd fdd Z e ddZ e j ddZ dfd d Z dd d ZZS)DatePatternRegexzDate template, with regex/pattern Parameters ---------- pattern : str Sets the date templates pattern. Attributes ---------- name regex pattern Nc s2tt|d|_|dur.|j|fi|dSr)superrar_patternr6)rrJkwargs __class__r rr!szDatePatternRegex.__init__cCs|jS)aVThe pattern used for regex with strptime "%" time fields. This should be a valid regular expression, of which matching string will be extracted from the log line. strptime style "%" fields will be replaced by appropriate regular expressions, or custom regex groups with names as per the strptime fields can also be used instead. )rcrr r rrJ's zDatePatternRegex.patterncCs||dSr)r6)rrJr r rrJ3sTc s||_t|r$td|}d}}|rBt|rBtd|}d}zL|jd|}||j|_|t}t |rxd|}t t | |||Wn4t y}ztd||fWYd}~n d}~00dS)Nr Fr z%(\1)sz(?iu)zWFailed to set datepattern '%s' (may be an invalid format or unescaped percent char): %s)rcrIr#r$rH _patternRE _patternNamerrRE_ALPHA_PATTERNrbrar6r: TypeError)rrJr4r5fmtr3r<rer rr67s      zDatePatternRegex.setRegexcCs*|s||}|r&t||d|fSdS)aMethod to return the date for a log line. This uses a custom version of strptime, using the named groups from the instances `pattern` property. Parameters ---------- line : str Log line, of which the date should be extracted from. default_tz: optionally used to correct timezone Returns ------- (float, str) Tuple containing a Unix timestamp, and the string of the date which was matched and in turned used to calculated the timestamp. )rDN)rAr groupdictrCr r rrENs  zDatePatternRegex.getDate)N)TT)NN)rLrMrNrOrrgrhr8r9rrPrJsetterr6rE __classcell__r r rerras    rac@s$eZdZdZdddZd ddZdS) DateTai64nz_A date template which matches TAI64N format timestamps. Attributes ---------- name regex FcCs"t|d|_|jd|ddS)NZTAI64Nz @[0-9a-f]{24}rW)rrrr6)rr4r r rrps zDateTai64n.__init__NcCs:|s||}|r6|d}|dd}t|d|fSdS)aqMethod to return the date for a log line. Parameters ---------- line : str Log line, of which the date should be extracted from. default_tz: ignored, since TAI is time zone independent Returns ------- (float, str) Tuple containing a Unix timestamp, and the string of the date which was matched and in turned used to calculated the timestamp. rrr\N)rAr int)rr>r@rDvalueZseconds_since_epochr r rrEvs    zDateTai64n.getDate)F)NNr`r r r rrogs ro)$ __author__Z __copyright__Z __license__r8rabcrstrptimerrrZhelpersrrLr1r9r'r(r"rIrHrGrFr%r&r0r-r/ri IGNORECASErZobjectrrRraror r r rs8             ?X