o c@sddlmZddlZddlZddlmZmZGdddeZGdddeZ Gdd d e Z Gd d d e Z Gd d d e Z dS))absolute_importN) format_deltaformat_percentc@s:eZdZdZd ddZddZddZd d Zd d ZdS)Timerzp A timer keeps track of the number of seconds passed during it's lifetime and since the last reset. NcCs"|ptj|_||_|j|_dSN)time _create_time_creation_time _last_time)self create_timer 7/usr/lib/python3/dist-packages/landscape/lib/monitor.py__init__s   zTimer.__init__cCs|Sr)rr r r rrsz Timer.timecC||jSr)rr rr r r since_startzTimer.since_startcCrrrr rr r r since_resetrzTimer.since_resetcCs||_dSrrrr r rresetrz Timer.resetr) __name__ __module__ __qualname____doc__rrrrrr r r rr s  rcs>eZdZdZd fdd ZddZfddZd d ZZS) Monitora A monitor tracks the number of pings it received during it's lifetime and since the last reset. The component being monitored is responsible for calling C{ping()} everytime a monitored activity occurs. It should register a reactor event that logs statistics from this monitor every N seconds. Essentially, monitors are just statistics checkers that components can use to monitor themselves. Ncs(tt|j|d||_d|_d|_dS)Nr r)superrr event_namecount total_count)r rr  __class__r rr,s zMonitor.__init__cCs |jd7_|jd7_dS)N)rr rr r rping2sz Monitor.pingcstt|d|_dS)Nr)rrrrrr!r rr6s z Monitor.resetcCs(td|j|jt||dS)Nz%%d %s events occurred in the last %s.)logginginforrrrrrr r rlog:s  z Monitor.logr) rrrrrr$rr' __classcell__r r r!rr!s   rcs8eZdZdZ d fdd ZfddZddZZS) BurstMonitorz A burst monitor tracks the volume pings it receives. It goes into warn mode when too many pings are received in a short period of time. Ncs*tt|j||d||_||_g|_dSNr)rr)rrepeat_interval maximum_count _last_times)r r+r,rr r!r rrGs zBurstMonitor.__init__cs^tt||}|j||jd||jks%t|j|jdkr-|j ddSdS)Nrr#) rr)r$rr-appendr+lenr,pop)r nowr!r rr$Ns zBurstMonitor.pingcCs:|jsdS||jd}||jkot|j|jdkS)NFrr#)r-rr+r/r,)r deltar r rwarnWs  zBurstMonitor.warnr)rrrrrr$r3r(r r r!rr)@s   r)csLeZdZdZ d fdd ZeddZeddZd d Zd d Z Z S)CoverageMonitora A coverage monitor tracks the volume of pings received since the last reset. It has normal and warn states that are determined by calculating the number of expected pings since the last reset. If the actual number of pings falls below the minimum required percent the monitor goes into warn mode. The component being monitored should register a reactor event that logs statistics from this monitor every N seconds. Ncs$tt|j||d||_||_dSr*)rr4rr+ min_percent)r r+r5rr r!r rrjs  zCoverageMonitor.__init__cCs(z |jt|jWStyYdSw)Ng?)rfloatexpected_countZeroDivisionErrorrr r rpercentqs  zCoverageMonitor.percentcCst||jSr)intrr+rr r rr7xszCoverageMonitor.expected_countc Cs^d}|jr |jr |jd}tj}|rtj}|d|j|j|jt|t | | dS)Ngdz9%d of %d expected %s events (%s) occurred in the last %s.) r9r7r%r&r3warningrrrrrr)r r9r'r r rr'|s    zCoverageMonitor.logcCs*|jr|jr|js dS|j|jkrdSdS)NFT)r+r5r7r9rr r rr3s  zCoverageMonitor.warnr) rrrrrpropertyr9r7r'r3r(r r r!rr4_s   r4csLeZdZdZ d fdd ZeddZfddZd d Zd d Z Z S)FrequencyMonitoraz A frequency monitor tracks the number of pings received during a fixed period of time. It has normal and warn states; a warn state is triggered when the minimum expected pings were not received during the specified interval. The component being monitored should register a reactor event that checks the warn state of this monitor every N seconds. Ncs.tt|j||d||_||_||_dSr*)rr>rr+ min_frequencyr _last_count)r r+r?rr r!r rrs zFrequencyMonitor.__init__cCs||j}||jSr)rr@r+)r since_pingr r rr7s zFrequencyMonitor.expected_countcstt|||_dSr)rr>r$rr@rr!r rr$szFrequencyMonitor.pingcCs2|rtd|j|j|jt|j|dS)NzAOnly %d of %d minimum expected %s events occurred in the last %s.) r3r%r<rr7rrr+rrr r rr's zFrequencyMonitor.logcCs4|jr|jr||j|jkr|j|jkrdSdS)NTF)r+r?rr@rrr r rr3s   zFrequencyMonitor.warnr) rrrrrr=r7r$r'r3r(r r r!rr>s   r>) __future__rr%rlandscape.lib.formatrrobjectrrr)r4r>r r r rs 4