|d#xdZddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z GddZ dZ dZ dS)zq statfiles.py - class for digesting and plotting NTP logfiles Requires gnuplot and liberation fonts installed. )print_functiondivisionNceZdZdZdZdZiZdZdZdZ dZ e dZ e dZ e dZe d Z dd Zd Zd Zd ZdZdZdS)NTPStatsz*Gather statistics for a specified NTP siteiQi: Ncvg}|D]} |}t|d}t|d}n#t$rYNwxYwtj|z|zdz }||cxkr|kr?nwt|dz|d<t ||d<|||S)zExtract first two fields, MJD and seconds past midnight. convert timestamp (MJD & seconds past midnight) to Unix time Replace MJD+second with Unix time.rl@")splitintfloat ValueErrorr SecondsInDaystrappend) lines starttimeendtimelines1liner mjdsecondtimes //usr/lib/python3/dist-packages/ntp/statfiles.pyunixizezNTPStats.unixizes  % %D  %(mmuQx     (3.7*DDD++++G+++++td{++at99a e$$$ s>A AAcPt|dS)z"get Unix time from converted line.r)r r )rs r timestampzNTPStats.timestamp9sTZZ\\!_%%%ci}t|}d|kr,d|kr |d}nd}|D]}||dt|z<nH|D]E}|dkr||dz |d<|t||dz z|dt|z<F|S)zwReturn given percentiles of a given row in a given set of entries. assuming values are already split and sortedr rpdp100)lenrr )percentsvaluesretlengthvaluepercs r percentileszNTPStats.percentiles>sV ;;F{{q   - -',C#d))O$$ -! L L3;;"(!"4CKK+1#fS6I2J2J+KCc$ii(( rc |drh|dr#|d\}}}}d|d|dS tj|\}}}|S#tj$rYnwxYw|S)z,Produce appropriate label for an IP address.rz127.127..zREFCLOCK(type=z,unit=))isdigit startswithr socket gethostbyaddrherror)key_ clock_typeunithostnames rip_labelzNTPStats.ip_labelVs q6>>    ~~j)) H+.99S>>(Az446@jj$$$GG #)#7#<#< 1a=     sA..B?Bc| tj}||_|(|&tt j}||z }n||||z }n ||||z}||_||_|ptj ||_ d|j krtj |_ tj |s1tjd|zt#dg|_g|_g|_g|_g|_g|_dD]?}|||}|||}t5|||@dS)z.Grab content of logfiles, sorted by timestamp.Nntpstatsz%ntpviz: ERROR: %s is not a directory r ) clockstats peerstats loopstatsrawstatstempsgpsd)r DefaultPeriodperiodr rrrospathbasenamesitenamer0getfqdnisdirsysstderrwrite SystemExitr;r<r=r>r?r@_NTPStats__load_stem_NTPStats__process_stemsetattr) selfstatsdirrFrBrrstemr processeds r__init__zNTPStats.__init__ms >+F  ?y0$)++&&G&(II  7#6&(II  "w&(G"  >BG$4$4X$>$>  & &"N,,DMw}}X&& J  E'( ) ) )Q--    2 + +D$$Xt44E++D%88I D$ * * * *  + +rc$g} tj||}|dkr |dkr|dz }tj|dzD]}|jtj|kr+|dr+|tj|d z }k|t|d z }n2#t$r%tj d|zYnwxYw|S) Nr?r@r,*gzrtrz#ntpviz: WARNING: could not read %s )rCrDjoinglobrgetmtimeendswithgzipopen readlinesIOErrorrIrJrK)rPrQrRrpatternlogparts r __load_stemzNTPStats.__load_stems, (gll8T22Gw46>>39Ws]33 < <>BG$4$4W$=$===##D))<TYw55??AAAEET'3//99;;;EE < ( ( ( J  C&' ( ( ( ( ( ( sCC,D  D cg}|dks|dkr|D]}|}dt|kr* t|d}n#t$rYMwxYw|j|cxkr |jkr>nk|dt|dz||n&t ||j|j}| |S)Nr?r@rr ) r r#r rrrinsertr rrrsort)rPrRrrrr time_floats r__process_stemzNTPStats.__process_stems 7??dfnn ) ) s5zz>>!&uQxJJ!H>Z????4<?????LLC T(9$:$:;;;MM%((( )&%%eT^T\JJF   sA AAc|jr|jS|jD]N} |d}||jvr g|j|<|j||?#t$rYKwxYw|jS)zoReturn a dictionary mapping peerstats IPs to entry subsets. This is very expensive, so cache the result)peermapr<r IndexError)rProw ip_addresss r peersplitzNTPStats.peersplits < < >  C  V T\11/1DL, Z(//4444    |s;A A"!A"ci}|jD]?} |d}||vrg||<|||0#t$rYrs0/////// ````````F(((FFFFFr