0}ddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z dZ dZ ee ZGddeZedZdS) N)contextmanager)ListOptionalz/var/lve/snapshotsz .snapshotceZdZdZddZdZedZdZdZ dZ d Z d Z d Z d Zd ZdZdZedZeddZedZdS)Snapshotad This class is responsible for loading and saving snapshot files for the interval The files will be saved in a format of: /var/lve/snapshots/[uid]/dump_time.snapshot dump_time is the timestamp/integer The directories /var/lve/snaphosts/[uid] and the dump files itself will be owned by user They will not be readable by other users. cT||_||_tjd|_dS)Nz lib-snapshot) compresslevelincidentlogging getLoggerlog)selfr r s lvestats/lib/snapshot.py__init__zSnapshot.__init__s'*  $^44c|d}|J||tjtj||j}||5}|||j d|j dddn #1swxYwY|j S)N dump_timezSnapshot dumped to file %s) _replace_unicode_datazlibcompressjsondumpsencoder create_filewriterdebugname)rdata dump_datejson_compressedfs rsavez Snapshot.save%s% $$$ ""4(((- 4(8(8(?(?(A(A4CUVV   i ( ( AA GGO $ $ $ HNN7 @ @ @ A A A A A A A A A A A A A A Av s56B77B;>B;c\t|tr|ddS|S)Nzutf-8replace) isinstancebytesdecode)objs r _to_unicodezSnapshot._to_unicode2s- c5 ! ! 2::gy11 1 rc Zg}|dgD]7}|tt|j|8||d<g}|dgD]7}|tt|j|8||d<dS)Nsnap_sql snap_http)getappendlistmapr*)rr u_queriesqueryu_urlshttps rrzSnapshot._replace_unicode_data8s XXj"-- A AE   T#d&6">">?? @ @ @ @$ZHH["-- = =D MM$s4#3T::;; < < < <"[rc|}tj|rtj|SgSN)get_dirospathisdirlistdir)rdir_s r get_file_listzSnapshot.get_file_listDs:||~~ 7==   $:d## # rcT||||S)z Return ordered list of timestamps when snapshots for this use were created :param from_ts: :param to_ts: :return: list of timesteamps, ordered for that period )snapshot_filterr>)rfrom_tsto_tss r get_ts_listzSnapshot.get_ts_listKs(##D$6$6$8$8'5IIIrc g}|||D]6} |||}tjsHt |jd5||}dddn #1swxYwYn||}|tj |#tttf$rZ}|jd|||t!|Yd}~0d}~wwxYw|S)z Get all snapshot for a period :param from_ts :param to_ts :return: list of snapshots, ordered by date uidNzCorrupted file: %s (%s))rC get_file_name ts_to_namer9geteuiddrop_privilegesr read_file_contentr/rloadsIOError ValueErrorOSErrorrwarningstr)rrArBresulttsfilenamecontentves r get_snapshotszSnapshot.get_snapshotsUs""7E22  B --doob.A.ABBz||?(u)=>>CC"&"8"8"B"BCCCCCCCCCCCCCCC#44X>>G dj112222Z1     -&&tr':':;;GG   s>AC2B CB CB ACE4AE  Ect|d5}|}dddn #1swxYwY tj|}nl#tj$rZtj||j}t|d5}||dddn #1swxYwYYnwxYw|S)Nrbwb) openreadr decompresserrorrr rr()rrSr"rTcompressed_contents rrJzSnapshot.read_file_contentosB (D ! ! QffhhG                ,og..GGz , , ,!%w8J!K!K h%% ,*+++ , , , , , , , , , , , , , , , ,~~sA266A9B< B." B<.B2 2B<5B2 6B<;B<cZ||jd|jdS)zZ Load all snapshots for given incident :return: list of snapshots incident_start_timeincident_end_time)rVr rs rget_incident_snapshotszSnapshot.get_incident_snapshots{s* !!$-0E"F ViHjkkkrc|tjtt |jdS)NrE)r9r:join SNAPSHOT_PATHrPr rbs rr8zSnapshot.get_dirs'w||M3t}U/C+D+DEEErcftj||Sr7)r9r:rer8)rrs rrFzSnapshot.get_file_names w||DLLNND111rc F|}tj|s tj|tj|dtj||jddnG#ttf$r3}|j d|t|Yd}~nd}~wwxYw| ||}t|jd5t!|d} tj|dnG#ttf$r3}|j d|t|Yd}~nd}~wwxYwdddn #1swxYwY|S) z create file, change its ownership & permissions if needed. Create directories if needed as well :param dump_date: int timestamp used as file name :return: open File object irErzUnable to create dir %s (%s)NrYz&Unable to set file permissions %s (%s))r8r9r:existsmakedirschmodchownr rLrNrr]rPrFrGrIrZfchmodfileno)rr r=e file_namefile_s rrzSnapshot.create_files||~~w~~d## M M D!!!u%%%t}U3Q7777W% M M M=tSVVLLLLLLLL M&&ty'A'ABB T]51 2 2 \ \D))E \ %,,..%0000W% \ \ \GTWXYTZTZ[[[[[[[[ \  \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  sTA BC)B??C F'EFF)F=FFFFFch|}tj|}||}|||}||krt j|ddS|D]<}tj|||=dS)z Delete old snapshots. If there are no more :param to_ts: up to which timestamp to remove snapshots :return: None )rBT) ignore_errorsN) r8r9r<r@shutilrmtreeremoverFrG)rrB_dirfiles all_snapshots ts_to_removerRs r delete_oldzSnapshot.delete_olds||~~ 4  ,,U33 ++E+?? L ( ( M$d 3 3 3 3 3 3" C C $,,T__R-@-@AABBBB C Crc|trD|dt }|r t |S#t $rYnwxYwdSNr)endswith SNAPSHOT_EXTSNAPSHOT_EXT_LENisdigitintrM)rrrRs rget_tszSnapshot.get_tssp >>, ' ' q***+Bzz|| r77N!DtsA AANc|d}| tj}g}|D]C}t|}|%||cxkr|krn.||Dt |Sr~)sysmaxsizerrr/sorted)ryrArBrQrSrRs rr@zSnapshot.snapshot_filters ?G =KE " "H**B~'R"8"8"8"85"8"8"8"8"8 b!!!f~~rc0t|tzSr7)rPr)rRs rrGzSnapshot.ts_to_names2ww%%r)r)NN)__name__ __module__ __qualname____doc__rr# staticmethodr*rr>rCrVrJrcr8rFrr|rr@rGrrrrsT5555   \ # # #JJJ4    lllFFF2220CCC"  \    \ &&\&&&rrc#,Ktjtjtj}}}t jdd}tjgtj|tj| dVtj|tj|tj|dS#tj|tj|tj|wxYw)Nnobody) r9getuidgetgid getgroupspwdgetpwnam setgroupssetegidseteuid)rEold_uidold_gid old_groupsgids rrIrIs#%9;; R\^^jWG ,x  #CLJsOOOJsOOO!  7 7 Z      7 7 Z    s C>D)rr r9rrurr contextlibrtypingrrrfrlenrobjectrrIrrrrs  %%%%%%!!!!!!!!$  3|$${&{&{&{&{&v{&{&{&| ! ! ! ! !r