Tag Values โ€‹โ€‹as Warning or Critical

I have a dictionary as follows (the dict is called channel_info):

{'flume02': u'98.94420000000001', 'flume03': u'32.562999999999995', 'flume01': u'2.15'} 

I am trying to scroll through a dictionary and tell the values โ€‹โ€‹warning or critical. I have arguments in the program

 parser.add_argument('-w', '--warning', type=int, help='Warning threshold', default=85) parser.add_argument('-c', '--critical', type=int, help='Critical threshold', default=95) 

so basically when I run a program like myprog.py -w 80 -c 90 , I want flume02 as critical (in this case it will be the only way out). If any other key had a value greater than 80 or 90, they would be reported as warning or critical, respectively.

However, this is not so, and I get all the values โ€‹โ€‹under critical.

Relevant Code:

  if args.warning and not args.critical: for each in channel_info.items(): if float(each[1]) > float(args.warning): print 'WARNING | {} is {} percent full'.format(*each) exit(1) if args.critical and not args.warning: for each in channel_info.items(): if float(each[1]) > float(args.critical): print 'CRITICAL | {} is {} percent full'.format(*each) exit(2) if args.warning and args.critical: for each in channel_info.items(): if float(args.warning) < each[1] < float(args.critical): print 'WARNING | {} is {} percent full'.format(*each) elif each[1] > float(args.critical): print 'CRITICAL | {} is {} percent full'.format(*each) 

Output:

 CRITICAL | flume02 is 99.9892 percent full CRITICAL | flume03 is 51.4497 percent full CRITICAL | flume01 is 7.95 percent full 

I set if the last condition is if ( if args.warning and args.critical ), to make sure that the program can work with 1 ( -w or -c ) or both arguments. Any help with what I'm doing wrong will be greatly appreciated.

+4
source share
2 answers

I decided. Turns out there were two problems.

  • as JL Peyret pinted out I was missing a float.
  • However, the main problem of -c did not work autonomously, so that both arguments had default values, and when I used only -c , the fist if statement was executed (because there is a value, and it was true), and all values โ€‹โ€‹are marked as a warning
0
source

I think you forgot the float (each [1]) on branch # 3. that is, a comparison of a string with a floating point.

Itโ€™s all the more sensible to format your comparison values โ€‹โ€‹only once, rather than doing float (xyz) every time.

 threshold_crit = 90.0 threshold_warn = 80.0 for each in channel_info.items(): # value = float(each[1]) value = each[1] if threshold_crit < value < threshold_warn: print 'WARNING | {} is {} percent full'.format(*each) elif value > threshold_crit: print 'CRITICAL | {} is {} percent full'.format(*each) 

exit:

  CRITICAL | flume02 is 98.94420000000001 percent full CRITICAL | flume03 is 32.562999999999995 percent full CRITICAL | flume01 is 2.15 percent full 

change code to:

  value = float(each[1]) #value = each[1] 

exit:

  CRITICAL | flume02 is 98.94420000000001 percent full 
+1
source

All Articles