U
    Tgc                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ zd dlmZ W n  ek
r   d dlmZ Y nX e Zeej i ZedddZd	Zd	Zd
Zdd Zdd Zdd Zdd Zdd Zdd Zdd Ze dkre Z!e"ddkr$e#d e $d e!j%r8eej e&e j'Z(e)e( e!j*Ze!j%Ze!j+Ze!j,Z-e!j.Z/e-d Z0e-d Z1e0gZ2e!j3re24d  e!j5re24d! ee!j6 ee!j7 dS )"    N)Pool	cpu_count)	ConfigObj)scandir)listdir.*z1m:7d)match
retentionsF c                 C   st   t | }| D ]^}|d d }|dkr:|d d td< q|d dkrR|dd  }|d d t|dt|< qd S )N   patternr   r	   r   ^)r	   r   )r   itemsDEFAULT_SCHEMArecompileSCHEMA_LIST)cfgZschema_confschemaitem r   d/home/ankuromar296_gmail_com/.local/lib/python3.8/site-packages/../../../bin/update-storage-times.pyconfig_schemas#   s    
r   c                 C   sP   dddddddd}|  d\}} t| d	 rLt| d d	 || d	   } | S )
Nr   <   i  iQ i:	 i3)smhmindwy:)splitstrisalphaint)timeZseconds_dictZpointsr   r   r   _convert_seconds1   s      r(   c                 C   sR   dd t | D }t|g}tj|tjd}| d d}dd |D }||kS )Nc                 S   s   g | ]}t |qS r   )r(   ).0r   r   r   r   
<listcomp><   s     z&_compare_retention.<locals>.<listcomp>stdoutr   
c                 S   s&   g | ]}d |krt |dd qS )	retentionr!   r   )r&   r#   )r)   liner   r   r   r*   @   s    )listINFO_BIN
subprocessPopenPIPEcommunicater#   )r.   Ztmp_pathZnew_retentionZinfo_stringZcur_ret_listZcur_retentionr   r   r   _compare_retention:   s    r6   c                 c   sX   t | D ]J}|jddr0t|jD ]
}|V  q"q|jddr|jdsJq|jV  qd S )NF)follow_symlinksz.wsp)r   is_dir_find_metricspathis_filenameendswith)r:   fZsfr   r   r   r9   E   s    

r9   c           	      C   s  t std g S tr$td|   ttjd}tt	| g }t
d }| ttd  dd}t  D ]"\}}|d |rf|d } qqf|t| trtd|  t|| rtd	|   d
| fgS trd}nVtd|  t| }trt|}ntj||d}t| |j t| |j|j |  tt |dkr|td|   td|  td|  d
| fgS d| fgS d S )NDidn't initialize schemas!zTesting %s for modificationr   r	   /.r   zCreated command: %sz$%s has the same retention as before!Fr   zRetention will be %sr+   zFailed to update schemas for %szAttempted retention: %szAttempted command string: %sT) r   LOGerrorDEBUGinfoopenosdevnullr0   BASE_COMMANDr   len	ROOT_PATHreplace	iteritemssearchextendr6   debugDRY_RUNstatr2   
check_callchmodst_modechownst_uidst_gidcloser'   sleepSLEEP)	ZmetricrH   Zcommand_stringr.   Zmatchingr   rE   resstr   r   r   
fix_metricQ   sL    






r^   c                 C   s^   t std d S tjt| }tt }t	d|  |
tt|d}|  |  |S )Nr?   z5Creating new storage schemas for metrics under %s ...d   )r   rB   rC   rG   r:   joinrK   r   r   rE   mapr^   r9   rY   )subdirZfpathpoolresultsr   r   r   search_and_fix   s    

re   c                  C   s   t d} | jdddddd | jddd	d
dd | jdddddd | jdddddd | jdddddd | jdddddd | jdddddd | jdddd d!d | jd"dtd#d$d%d& |  S )'Nz2Correct storage settings on multiple whisper filesz--cfgstorer   z"The storage-schemas.conf file pathT)actiondesthelprequiredz--pathr:   z The root path to find metrics inz--debug
store_truerP   zDisplay debug informationF)rg   rh   ri   defaultz	--dry-rundry_runzDon't actually do anythingz--subdirrb   z/If you only want to process a particular subdirr
   
--nobackupnobackupz:Passed through to whisper-resize.py, don't create a backup--aggregate	aggregatez3Passed through to whisper-resize.py, roll up valuesz--bindirbindirz6The root path to whisper-resize.py and whisper-info.pyz/opt/graphite/binz--sleeprZ   z?Sleep this amount of time in seconds between metric comparisonsg333333?)rg   typerh   ri   rl   )argparseArgumentParseradd_argumentfloat
parse_args)parserr   r   r   cli_opts   sL    








rz   __main__USERrootz!You must run this script as root!r   z/whisper-resize.pyz/whisper-info.pyrn   rp   )8sysrG   loggingr2   rt   r   r'   multiprocessingr   r   Z	configobjr   r   ImportErrorr   	getLoggerrB   setLevelINFOr   r   r   rD   rQ   rK   r   r(   r6   r9   r^   re   rz   __name__Zi_argsgetenvprintexitrP   StreamHandlerr,   Zsoh
addHandlerr:   rm   rr   BINDIRrZ   r[   Z
RESIZE_BINr1   rI   ro   appendrq   r   rb   r   r   r   r   <module>	   sf   	7 





