<s> import argparse <EOL> import decimal <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> sys . path . extend ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> os . environ . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> from django . db . models . loading import cache <EOL> cache . get_apps ( ) <EOL> from freenasUI . common . system import send_mail <EOL> TO_MB = { <EOL> '<STR_LIT:T>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:M>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : decimal . Decimal ( '<STR_LIT>' ) , <EOL> } <EOL> def to_mbytes ( string ) : <EOL> unit = string [ - <NUM_LIT:1> ] . upper ( ) <EOL> value = decimal . Decimal ( string [ : - <NUM_LIT:1> ] ) <EOL> return value * TO_MB . get ( unit , <NUM_LIT:1> ) <EOL> def email ( dataset , threshold , avail ) : <EOL> send_mail ( subject = "<STR_LIT>" , <EOL> text = """<STR_LIT>""" % ( dataset , threshold , avail ) ) <EOL> def _size_or_perc ( string ) : <EOL> last = string [ - <NUM_LIT:1> ] <EOL> if last . upper ( ) not in ( '<STR_LIT:T>' , '<STR_LIT>' , '<STR_LIT:M>' , '<STR_LIT>' , '<STR_LIT:%>' ) : <EOL> raise argparse . ArgumentTypeError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> return string <EOL> def main ( argv ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required = True , <EOL> type = str , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required = True , <EOL> type = _size_or_perc , <EOL> ) <EOL> args = parser . parse_args ( argv ) <EOL> pipe = subprocess . Popen ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:list>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> stdout = subprocess . PIPE , <EOL> stdin = subprocess . PIPE ) <EOL> output = pipe . communicate ( ) [ <NUM_LIT:0> ] <EOL> if pipe . returncode != <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> reg = re . search ( r'<STR_LIT>' % ( args . dataset , ) , output , re . M ) <EOL> if not reg : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> line = reg . group ( <NUM_LIT:0> ) <EOL> o_used , o_avail , o_refer = line . split ( '<STR_LIT:\t>' ) [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> used = to_mbytes ( o_used ) <EOL> avail = to_mbytes ( o_avail ) <EOL> if args . threshold [ - <NUM_LIT:1> ] == '<STR_LIT:%>' : <EOL> threshold = ( used + avail ) * decimal . Decimal ( args . threshold [ : - <NUM_LIT:1> ] ) / <NUM_LIT:100> <EOL> else : <EOL> threshold = to_mbytes ( args . threshold ) <EOL> sentinel_file = "<STR_LIT>" % ( <EOL> args . dataset . replace ( '<STR_LIT:/>' , '<STR_LIT:_>' ) , <EOL> ) <EOL> if avail < threshold : <EOL> if not os . path . exists ( sentinel_file ) : <EOL> email ( args . dataset , args . threshold , o_avail ) <EOL> open ( sentinel_file , '<STR_LIT:w>' ) . close ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> else : <EOL> try : <EOL> os . unlink ( sentinel_file ) <EOL> except : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
