{'flume02': u'98.94420000000001','flume03': u'32.562999999999995','flume01': u'2.15'}
我试图循环通过字典并报告值警告或关键.我不得不参与该计划
parser.add_argument('-w','--warning',type=int,help='Warning threshold',default=85)parser.add_argument('-c','--critical',help='Critical threshold',default=95)
所以基本上当我运行像myprog.py -w 80 -c 90这样的程序时,我想要
flume02是关键的(在这种情况下,这将是唯一的输出).如果任何其他密钥的值大于80或90,则它们将分别报告为警告或关键.
然而事实并非如此,我认为所有的价值都是关键的.
相关代码:
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)
输出:
CRITICAL | flume02 is 99.9892 percent fullCRITICAL | flume03 is 51.4497 percent fullCRITICAL | flume01 is 7.95 percent full
我把if if条件(如果是args.warning和args.critical)放在一起,以确保程序能够以1(-w或-c)或两个参数运行.任何有关我做错的任何帮助将不胜感激
解决方法 我想你忘记了#3分支上的浮动(每个[1]).即比较字符串到浮点数.更有理由将比较值格式化一次,而不是每次都执行浮点数(xyz).
threshold_crit = 90.0threshold_warn = 80.0for 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)
输出:
CRITICAL | flume02 is 98.94420000000001 percent full CRITICAL | flume03 is 32.562999999999995 percent full CRITICAL | flume01 is 2.15 percent full
将代码更改为:
value = float(each[1]) #value = each[1]
输出:
CRITICAL | flume02 is 98.94420000000001 percent full总结
以上是内存溢出为你收集整理的python – 将值标记为警告或关键全部内容,希望文章能够帮你解决python – 将值标记为警告或关键所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)