TypeError:字符串索引必须是整数 - check_dell_warrant.py

时间:2014-01-20 19:13:16

标签: python nagios

我正在使用为Nagios开发的插件,但在某些主机上运行时出现错误。我通常会调试但对python编程很新。我得到的错误是:

Traceback (most recent call last):
  File "/usr/lib64/nagios/plugins/check_dell_warranty.py", line 755, in <module>
  parse_exit(RESULT, options.short_output)
 File "/usr/lib64/nagios/plugins/check_dell_warranty.py", line 589, in parse_exit
  days, short_output)
File "/usr/lib64/nagios/plugins/check_dell_warranty.py", line 557, in process_asset
days, short_output)
File "/usr/lib64/nagios/plugins/check_dell_warranty.py", line 497, in build_warranty_line
 description = warranty['ServiceLevelDescription']
TypeError: string indices must be integers

脚本中的代码显示:

def build_warranty_line(warranty, full_line, days, short_output):
'''
This function takes a warranty object and parses the salient information
out. It then calculates the number of days remaining in the warranty
period, and builds a line for Nagios outputting.
'''

description = warranty['ServiceLevelDescription']
end_date = warranty['EndDate']
start_date = warranty['StartDate']
provider = warranty['ServiceProvider']

logger.debug('Found: Start date: {0}, End Date: {1},Description: {2}, '
             'Provider: {3}'.format(start_date, end_date, description,
                                    provider))

如果我在调试模式下运行脚本,它会显示传递给此函数的原始数据:

DEBUG - Attempting to load libsmbios_c.
DEBUG - Unable to load libsmbios_c continuing.
DEBUG - Obtaining serial number via OpenManage.
DEBUG - Requesting service tags: GSF0YX1
DEBUG - Requesting warranty information from Dell url:            
https://api.dell.com/support/v2/assetinfo/warranty/tags.json?      
apikey=1adecee8a60444738f280aad1cd87d0e&svctags=GSF0YX1
DEBUG - Raw output received:
 {u'GetAssetWarrantyResponse': {u'@xmlns': u'http://tempuri.org/',      u'GetAssetWarrantyResult': {u'Faults': None, u'@a': u'http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset', u'@i': u'http://www.w3.org/2001/XMLSchema-instance', u'Response': {u'DellAsset': {u'MachineDescription': u'PowerEdge R720', u'ShipDate': u'2013-05-08T00:00:00', u'ServiceTag': u'GSF0YX1', u'OrderNumber': 68701126, u'LocalChannel': u'IRL', u'AssetParts': {u'@nil': u'true'}, u'CountryLookupCode': 808, u'ItemClassCode': u'TE002', u'IsDuplicate':  u'false', u'ParentServiceTag': {u'@nil': u'true'}, u'CustomerNumber': 5503647,  u'Warranties': {u'Warranty': {u'StartDate': u'2013-05-08T00:00:00', u'EndDate': u'2016-05- 08T23:59:59', u'ServiceProvider': {u'@nil': u'true'}, u'ServiceLevelCode': u'ND', u'ItemNumber': u'709-11115', u'EntitlementType': u'INITIAL', u'ServiceLevelDescription': u'Next Business Day', u'ServiceLevelGroup': 99999}}}}}}}
DEBUG - Testing for faults in json response.
DEBUG - Raw fault return: None
DEBUG - No faults found.
DEBUG - Beginning to parse results and construct exit line and code.
DEBUG - Assets obtained: {u'MachineDescription': u'PowerEdge R720', u'ShipDate': u'2013-05-08T00:00:00', u'ServiceTag': u'GSF0YX1', u'OrderNumber': 68701126, u'LocalChannel': u'IRL', u'AssetParts': {u'@nil': u'true'}, u'CountryLookupCode': 808, u'ItemClassCode': u'TE002', u'IsDuplicate': u'false', u'ParentServiceTag': {u'@nil': u'true'}, u'CustomerNumber': 5503647, u'Warranties': {u'Warranty': {u'StartDate': u'2013-05-08T00:00:00', u'EndDate': u'2016-05-08T23:59:59', u'ServiceProvider': {u'@nil': u'true'}, u'ServiceLevelCode': u'ND', u'ItemNumber': u'709-11115', u'EntitlementType': u'INITIAL', u'ServiceLevelDescription': u'Next Business Day', u'ServiceLevelGroup': 99999}}}
DEBUG - A single asset is being processed.
DEBUG - Raw asset being processed: {u'MachineDescription': u'PowerEdge R720', u'ShipDate': u'2013-05-08T00:00:00', u'ServiceTag': u'GSF0YX1', u'OrderNumber': 68701126, u'LocalChannel': u'IRL', u'AssetParts': {u'@nil': u'true'}, u'CountryLookupCode': 808, u'ItemClassCode': u'TE002', u'IsDuplicate': u'false', u'ParentServiceTag': {u'@nil': u'true'}, u'CustomerNumber': 5503647, u'Warranties': {u'Warranty': {u'StartDate': u'2013-05-08T00:00:00', u'EndDate': u'2016-05-08T23:59:59', u'ServiceProvider': {u'@nil': u'true'}, u'ServiceLevelCode': u'ND', u'ItemNumber': u'709-11115', u'EntitlementType': u'INITIAL', u'ServiceLevelDescription': u'Next Business Day', u'ServiceLevelGroup': 99999}}}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您必须向我们显示对build_warranty_line的实际调用,但您的错误意味着warranty是字符串,而不是字典。你有json.loads JSON回复吗?