,IVsaddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z dZ dZeeZGddZe d efd ZdS) N)Iterator)contextmanager)BinaryIOz/var/lve/snapshotsz .snapshotc eZdZdZd dededdfdZdedefd Ze d Z deddfd Z de efd Z d edzdedzde efdZd edzdedzdeefdZdedefdZde fdZdefdZdedefdZdedefdZdeddfdZe dededzfdZe d!de ed edzdedzde efdZe dedefdZdS)"Snapshotaj Class 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/snapshots/[uid]` and the dump files themselves will be owned by user. They will not be readable by other users. incident compresslevelreturnNcT||_||_tjd|_dS)Nz lib-snapshot)r r logging getLoggerlog)selfr r s /builddir/build/BUILDROOT/lve-stats-4.3.2-1.el9.x86_64/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/lib/snapshot.py__init__zSnapshot.__init__#s'*  $^44datac|d}|J||tjtj||j}||5}|||j d|j dddn #1swxYwY|j S)N dump_timezSnapshot dumped to file %s) _replace_unicode_datazlibcompressjsondumpsencoder create_filewriterdebugname)rr 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_unicode4s- 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_data:s 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_listEs:||~~ 7==   $:d## # rfrom_tsto_tscT||||S)a Return ordered list of timestamps when snapshots for this user were created. :param from_ts: Start timestamp for filtering snapshots (inclusive). If None, starts from the earliest snapshot. :param to_ts: End timestamp for filtering snapshots (inclusive). If None, includes all snapshots up to the latest. :return: List of timestamps ordered for the given period. )snapshot_filterr?)rr@rAs r get_ts_listzSnapshot.get_ts_listKs(##D$6$6$8$8'5IIIrc #|K|||D]!} |||}tjsHt |jd5||}dddn #1swxYwYn||}tj |}|V#ttf$rZ}|j d|||t|Yd}~d}~wwxYwdS)a Get snapshots for a given period. Yields one snapshot at a time for memory efficiency. :param from_ts: Start timestamp for filtering snapshots (inclusive). If None, starts from the earliest snapshot. :param to_ts: End timestamp for filtering snapshots (inclusive). If None, includes all snapshots up to the latest. :return: Iterator of snapshot dictionaries. uidNzCorrupted file: %s (%s))rD get_file_name ts_to_namer:geteuiddrop_privilegesr read_file_contentrloads ValueErrorOSErrorrwarningstr)rr@rAtsfilenamecontent snapshot_dataves r get_snapshotszSnapshot.get_snapshots[s ""7E22  B --doob.A.ABB z||?(u)=>>CC"&"8"8"B"BCCCCCCCCCCCCCCC#44X>>G $ 7 3 3 #####(     -&&tr':':;;GG #  s=AC2B CB CB 1CD9AD44D9rRct|d5}|}dddn #1swxYwY tj|}nl#tj$rZtj||j}t|d5}||dddn #1swxYwYYnwxYw|S)Nrbwb) openreadr decompresserrorrr rr))rrRr#rScompressed_contents rrKzSnapshot.read_file_contentsB (D ! ! QffhhG                ,og..GGz , , ,!%w8J!K!K h%% ,*+++ , , , , , , , , , , , , , , , ,~~sA266A9B< B." B<.B2 2B<5B2 6B<;B<ctt||jd|jdS)zZ Load all snapshots for given incident :return: list of snapshots incident_start_timeincident_end_time)r1rVr rs rget_incident_snapshotszSnapshot.get_incident_snapshotss2 D&&t}5J'KT][nMoppqqqrc|tjtt |jdS)NrF)r:r;join SNAPSHOT_PATHrPr rbs rr9zSnapshot.get_dirs'w||M3t}U/C+D+DEEErr cftj||Sr8)r:r;rer9)rr s rrGzSnapshot.get_file_names w||DLLNND111rr!c *|}tj|s tj|tj|dtj||jddn@#t$r3}|j d|t|Yd}~nd}~wwxYw| | |}t|jd5t|d} tj|dn@#t$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 irFrzUnable to create dir %s (%s)NrYz&Unable to set file permissions %s (%s))r9r:r;existsmakedirschmodchownr rNrr]rPrGrHrJrZfchmodfileno)rr!r>e file_namefile_s rrzSnapshot.create_files ||~~w~~d## M M D!!! u%%%t}U3Q7777 M M M=tSVVLLLLLLLL M&&ty'A'ABB T]51 2 2 \ \D))E \ %,,..%0000 \ \ \GTWXYTZTZ[[[[[[[[ \  \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  sTA B B= )B88B=F'D<;F< E9)E4/F4E99FF F ch|}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 )rAT) ignore_errorsN) r9r:r=rCshutilrmtreeremoverGrH)rrA_dirfiles all_snapshots ts_to_removerQs r delete_oldzSnapshot.delete_olds ||~~ 4  ,,U33 ++E+?? L ( ( M$d 3 3 3 3 3 3" C C $,,T__R-@-@AABBBB C Crrrc|trD|dt }|r t |S#t $rYnwxYwdSNr)endswith SNAPSHOT_EXTSNAPSHOT_EXT_LENisdigitintrM)rrrQs rget_tszSnapshot.get_tssn >>, ' ' q***+Bzz|| r77N!DtsA AAryc|d}| tj}g}|D]C}t|}|%||cxkr|krn.||Dt |Sr~)sysmaxsizerrr0sorted)ryr@rAresultrRrQs rrCzSnapshot.snapshot_filters ?G =KE " "H**B~'R"8"8"8"85"8"8"8"8"8 b!!!f~~rrQc0t|tzSr8)rPr)rQs rrHzSnapshot.ts_to_names2ww%%r)r)NN)__name__ __module__ __qualname____doc__dictrrrPr$ staticmethodr+rr1r?rDrrVrKrcr9rGrrr|rrCrHrrrrs  555c5$5555  #    \ #$ #4 # # # #tCy JtJTzJ c JJJJ &t&Tz& $ &&&&P  #  #     rrrrrFFFFF2#2#2222SX>CCCCCC ccDj\# CytTz c \ &s&s&&&\&&&rrrFc#,Ktjtjtj}}}t jdd}tjgtj|tj| dVtj|tj|tj|dS#tj|tj|tj|wxYw)Nnobody) r:getuidgetgid getgroupspwdgetpwnam setgroupssetegidseteuid)rFold_uidold_gid old_groupsgids rrJrJs#%9;; R\^^jWG ,x  #CLJsOOOJsOOO!  7 7 Z      7 7 Z    s C>D)rr r:rrurrcollections.abcr contextlibrtypingrrfrlenrrrrJrrrrs  $$$$$$%%%%%%$  3|$$Q&Q&Q&Q&Q&Q&Q&Q&h ! ! ! ! ! ! !r