veg"> 0dZddlZddlZddlZddlZddlZddlZddlmcm Z ddl m Z ddl mZddlmZmZddlmZddlmZmZmZmZmZmZmZddlmZmZdd lmZddlZdd l m!Z!dd l"m#Z#dd l$m%Z%dd l&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-ddl.m/Z/ddl0m1Z1ddl+m2Z2ddl3m4Z4ddl5m6Z6ej7dZ8e dddgZ9de:de9fdZ;dee:fdZdEd e:d!e?de:fd"Z@d#e:de:fd$ZAd%e:dd&fd'ZBde:fd(ZCdFd*e:de:fd+ZDde:fd,ZEd-eFdeGfd.ZHd/e:dee:fd0ZIde?fd1ZJde?fd2ZKde?fd3ZLd4e?ddfd5ZMde?fd6ZNde:fd7ZOd8e:d9ee:efde?fd:ZPd;ZQedGd This module contains helpful utility functions for SSA Agent N) namedtuple)contextmanager)date timedelta) LooseVersion)socketfromfdAF_UNIX SOCK_STREAMAF_INETAF_INET6 SOCK_DGRAM)OptionalUnion)urlparse)AtexitIntegration)LoggingIntegration)Feature)is_panel_feature_supportedget_cp_descriptionis_throttling_supportedis_wp2_environment)get_kmodlve_module_version get_username)get_cl_edition_readable) get_hostname)get_rhn_systemid_value) sentry_dsn)SSAErrorutilsURL domain_nameuri_pathurlreturnct|}|jr d|jnd}|jr |j|nd}t|jdd|S)z Split URL into domain_name and uripath including query string :param url: URL of format protocol://domain/path;parameters?query#fragment :return: namedtuple URL(domain_name, uripath) ?/zwww.)rquerypathr"netlocreplace)r% fragmentsqsuris I/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/internal/utils.py url_splitr34sl  I"+/ 9 Y_   rB%.^ s r2add_infozsentry_init..add_infoUs& g}l;<<< r4c t|t5}||df|dcdddS#1swxYwYdS#t$rt dYdSwxYw)aI address_family - we can choose constants represent the address (and protocol) families (AF_INET for ipv4 and AF_INET6 for ipv6) private_ip - specify some private ip address. For instance: ipv4 -> 10.255.255.255 or ipv6 -> fc00:: rrNzCannot retrieve IP address)rrconnect getsockname Exceptionloggerinfo)address_family private_ipss r2 try_get_ipzsentry_init..try_get_ip_s 6 33 *q :q/***}}q) * * * * * * * * * * * * * * * * * * 6 6 6 KK4 5 5 5 5 5 5 6s4A"1A A"AA"AA""$B  B c`tdftdff}|D]\}}||}|r|cSdS)zq We are trying to get an IPv4 or IPv6 address. In case of failure we'll return 127.0.0.1 z10.255.255.255zfc00::z 127.0.0.1)r r ) ipversionsaddr_fampriv_ipiprLs r2get_ipzsentry_init..get_ipnsZ /08X2FF !+   HgHg..B   {r4c t}|r|dnd}|r|dnd}trdnd}dtpdfd|fd|fd|fd t jfd t d fd tfd t dfdfdtff }|D] }|j | dS)NversionnameWP2z alt-php-xrayUNKNOWNzControl Panel NamezControl Panel VersionzControl Panel ProductkernelzCloudLinux version os_releasezCloudlinux edition Architecture architecture ip_addressusername) rgetrr;platformreleaserrrset_tag) sentry_scopecp_description cp_versioncp_name cp_producttagstagrRs r2set_tagszsentry_init..set_tagszs+--6DN^'' 222$ 0>H.$$V,,,D022'@'@A!7!G!GHvvxx(\^^,  ' 'C L # & & & ' 'r4NcdSNr8)pendingtimeouts r2nopezsentry_init..nopesr4zalt-php-ssa@0.4-14.el8)level event_level)callbacki')dsn before_sendr`max_value_length integrationsid system_idr&N)dictr9rloggingINFOWARNINGr sentry_sdkinitrconfigure_scoperrruserrF) rBrirnssa_versentry_logging silent_atexitscoperRrLs @@r2 sentry_initrIsDT 6 6 6     ''''',-,,,mm77G'gl4;OEEEN%t444MO #%*"0-!@BBBB  # % % (55ee\^^eWcWeWe    HUOOOO    D  s6%=D # C/.D / C<9D ;C<<D  DDFfnameas_errorcL tj|tjdd tj|dn#t $rYnwxYw|S#t $rL}t|r tj n tjdt|Yd}~dSd}~wwxYw)zz Try to configure logging into given fname If as_error True, log the exception as ERROR, otherwise -- as INFO z%(asctime)s %(message)sz%m/%d/%Y %I:%M:%S %p)filenameroformatdatefmtiz$No logging configuration applied: %sN) rz basicConfigr{oschmodPermissionErrorOSErrorrGlogERRORstr)rres r2set_logging_into_filers U',#<$: < < < <  HUE " " " "    D   H>7==',9q66         s8"A ;A AA AA B#ABB#lognamecHtt| tjtj|n@#t $r3}tdt|Yd}~dSd}~wwxYwt|d}|S)zP Configure logging :param logname: path to log :return: logpath NzFailed to create logdir %sr)T)r) rrrmakedirsr,dirnamerFrGwarningr)rrs r2configure_loggingrs MMMW%%-  K00 1 1 1 1    NN7Q @ @ @22222 ($??? Ns1A B(B  B sock_locationz socket objectcttjdd}|dkrdt 5t t }|||dddn #1swxYwYn/tdt t}||S)z Create world-writable socket in given sock_location or reuse existing one :param sock_location: socket address :return: socket object LISTEN_FDSrN) intrenvironr^umask_0rr bindlistenr r )rrsockobjs r2 create_socketrsRZ^^L!4455JQ YY  WooG LL ' ' ' NN                   G[11 Ns>B  BBcttjtdz }|dS)zC Returns date of previous day in a format "day.month.year" r)days%d.%m.%Y)rtodayrstrftime) yesterdays r2previous_day_daters3 ya0000I   j ) ))r4rdatestrcltj||}|dS)z+ Convert date to format YYYY-mm-dd z%Y-%m-%d)datetimestrptimer)r formatstr_dates r2 format_daters.   & &w : :E >>* % %%r4c tjd}|}|dj}t j5}|d|dddn #1swxYwY|dS#ttj f$r3}t dt|Yd}~dSd}~wwxYw)ze Obtain system ID from /etc/sysconfig/rhn/systemid :return: system ID without ID- prefix z/etc/sysconfig/rhn/systemidz(.//member[name='system_id']/value/stringrwNzID-z Failed to retrieve system_id: %s)ETparsegetrootfindtextr}rralstripr ParseErrorrGrr)treerootwhole_idrrs r2 read_sys_idrs Cx566||~~99GHHM  ' ) ) 1U MM+x 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1u%%% R] #CCC93q66BBBBBBBBBCs<ABA:. B:A>>BA>BC#0(CC#durationcBtt|dz dS)zQ Cast duration from microseconds to seconds leaving 2 digits after point i@Bz0.2f)floatr)rs r2 duration_castrs! (&11 2 22r4filepathc& t|5}|}dddn #1swxYwYn#t$rYdSwxYwd|dddpdS)zDGet version of package from file. alt-php-ssa/alt-php-xray supportedN.z0.0-0)openreadstriprjoinsplit)rv_filerTs r2r7r7s (^^ ,vkkmm))++G , , , , , , , , , , , , , , ,  88GMM#&&rr* + + 6w6s3A'A AAA A A AAct}|dS t|tdkS#ttf$rYdSwxYw)zb Check version of alt-php-xray package. Autotracing in X-Ray is supported since 0.4-1 NFz0.4-1)r;r TypeErrorAttributeError)version_numbers r2is_xray_version_supportedrs_ "^^NuN++|G/D/DDD ~ &uus4A A cd}ttt5} ||n##tt f$rYddddSwxYw dddn #1swxYwYdS)z Check if User Agent is listeningz$/opt/alt/php-xray/run/xray-user.sockNFT)rr r rDConnectionErrorr)user_agent_sockrKs r2is_xray_user_agent_activers>, - -t X(# . . /*z(())Q. / / / / / / / / / / / / / / / / / / 9uus4A9%A, A9,A00A93A04A99B  B enabledcttjsdStd| t ddd5}||rdndtd ddddS#1swxYwYdS#t$r4}td |t|Yd}~dSd}~wwxYw) zb Switch on/off throttle statistics gathering by kmodlve :param enabled: True or False NzSwitching schedstats: %sz!/proc/sys/kernel/sched_schedstatswbr)mode buffering10zDone OKz(Failed to set sched_schedstats to %s: %s) rrLVErGrHrwriterr)rfrs r2switch_schedstatsr9sD &gk 2 2 KK*G444% 5D #"# GGG-DD . . . KK " " " # # # # # # # # # # # # # # # # # # %%% >SVV % % % % % % % % %%s;B 4B > B BBBB C")CCctd}td}t}|rt||Stt|S)z Check kmodlve module version or kernel version in order to determine if it provides the detection of IO throttling z2.0-23z1.5.58)rrcompare_versions_geextract_kernel_version)kmod_min_versionkernel_min_version kmod_currents r2$is_io_throttling_detection_availablerMs] $H--%h//-//LC"<1ABBB 5779K L LLr4ctjd}tj} ||d}n#t $rYdSwxYw|S)zO Get kernel version in the form of "major-minor" from current platform zlve([0-9]+(\.[0-9]+)+)\.elrz0.0.0)recompiler_r`searchgroupr)patternr` version_nums r2rrZspj677G  GnnW--33A66 ww s(A A A  first_versionsecond_versioncz t||kS#t$r}td|d|dd}~wwxYw)zE Comparing two versions using the greater or equal operator. zAUnable to compare required versions: unexpected versions format "z" and ""N)rrr )rrrs r2rrgskyM**n<< yyy wP] w wft w w wyy yys :5:c>tsdStS)zR General check of kernel support (IO throttling availability is required) T)rrr8r4r2is_kernel_version_supportedrrs# # $ $t / 1 11r4maskc#bKtj|}dVtj|dS)z, Context manager for dropping umask N)rumask)rprevs r2rr~s/ 8D>>D EEEHTNNNNNr4rT target_uid target_gidc#Ktj}tj} tj|}n#t$rd}YnwxYw| ||}n|j}| ||}n|j}|tj|}||krWtj|t d||r&tj |krtd||krqtj |t d||r@tj|kr)||krtj|tddV||kr/tj |t d|||kr/tj|t d||tj|dSdS)aH Context manager to drop privileges during some operation and then restore them back. If target_uid or target_gid are given, use input values. Otherwise, stat target_uid and target_gid from given target_path. If no target_path given, use current directory. Use mask if given. :param target_uid: uid to set :param target_gid: gid to set :param target_path: directory or file to stat for privileges, default -- current directory :param mask: umask to use :param with_check: check the result of switching privileges NzDropped GID privs to %sz6Unable to execute required operation: permission issuezDropped UID privs to %szRestored UID privs to %szRestored GID privs to %s)rgetuidgetgidstatrst_uidst_gidrsetegidrGdebuggetegidr seteuidgeteuid) rr target_pathr with_checkprev_uidprev_gid stat_infors r2set_privilegesrs"y{{Hy{{HGK((    !JJ")J  !JJ")J x~~: : . ;;;  J"*,,*44HJJ J: : . ;;;  J"*,,*44:%% 8$$$HJJ J EEE: 8 /:::: 8 /:::  s? A Ac2Gdd}||S)NceZdZdZdZdZdS)singleton..__Singletonzm A singleton wrapper class. Its instances would be created for each decorated class. c"||_d|_dSrk)_wrapped _instance)self_clss r2__init__z'singleton..__Singleton.__init__s DM!DNNNr4cP|jts|j|i||_|jS)z,Returns a single instance of decorated class)rIS_SINGLETON_ENABLEDr)rargskwargss r2__call__z'singleton..__Singleton.__call__s0~%-A%!.!?!?!?> !r4N)__name__ __module__ __qualname____doc__r r%r8r4r2 __Singletonrs<   " " " " " " " "r4r*r8)some_clsr*s r2 singletonr,s<"""""""" ;x  r4rx)F)r)r)NNrNT)Vr)rrrzrr_rxml.etree.ElementTreeetree ElementTreer collectionsr contextlibrrrdistutils.versionrrr r r r r rtypingrr urllib.parserr}sentry_sdk.integrations.atexitrsentry_sdk.integrations.loggingrclcommon.constrclcommon.cpapirrrrclcommon.utilsrrclcommon.lib.cleditionrclcommon.lib.networkrr constantsr exceptionsr getLoggerrGr"rr3r9r;rboolrrrrrrrrrr7rrrrrrrrrrr"r,r8r4r2r@s   """""""""""""""%%%%%%$$$$$$$$******""""""""!!!!!!<<<<<<>>>>>>"""""" DCCCCCCC::::::------111111!!!!!!  7 # #j 344 :3 :3 : : : :6Xc]6666:hsm:::: ZZZZz(ss"&*3****&&&s&&&& CS C C C C 3C3E33337#7(3-7777 4    4 d     %t%%%%%( Md M M M M     ysyE#|BS