2014-01-20 134 views
0

我使用Nagios的开发的插件,但在特定的主机上运行时,我得到一个错误。我通常会进行调试,但是对于python编程来说是非常新的。我得到的错误是:类型错误:字符串索引必须是整数 - check_dell_warrant.py

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}}} 

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

回答

2

您必须向我们显示实际致电build_warranty_line,但您的错误意味着warranty是一个字符串,而不是字典。 JSON响应是否json.loads

+0

我实际上并没有自己编写脚本,但我使用的版本在这里:[link](https://gitorious.org/smarmy/check_dell_warranty/source/b6438fbef45ba22be3bf0aa2e0aa2e444a384813:check_dell_warranty.py) – user3216306

相关问题