Collection of CheckMK checks (see https://checkmk.com/). All checks and plugins are provided as is. Absolutely no warranty. Send any comments to thl-cmk[at]outlook[dot]com

Skip to content
Snippets Groups Projects
Commit 955319b0 authored by thl-cmk's avatar thl-cmk :flag_na:
Browse files

Delete snmp_cisco_contract

parent f8fd2ff0
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
#
# License: GNU General Public License v2
#
# Author: thl-cmk[at]outlook[dot]com
# URL : https://thl-cmk.hopto.org
# Date : 2027-03-20
#
# collect/show Cisco hardware contract status
#
# 2017-05-29 : fixed empty pid handling
# added serial number cleanup
# 2018-04-09 : removed import
# 2018-09-04 : changes for CMK 1.5.x (inv_tree --> inv_tree_list)
# 2018-09-05 : changes for CMK 1.5.x (replaced global variable g_hostname with api call host_name())
# 2019-08-13 : chaged variable host_anme to _hostname for cmk1.5
# 2020-08-04 : moved node tree from hardware.system.support to hardware.system
# cleanup create_sn2info_record
#
# import is done by Check_MK
# import logging
# import os
import json
import ConfigParser
def create_sn2info_record(sn2infofile, optional_columns):
sn2info = {}
if os.path.isfile(sn2infofile):
with open(sn2infofile) as f:
sn2inforecord = json.load(f)
modifytime = os.path.getmtime(sn2infofile)
sn2info.update({'Last_checked': time.strftime('%Y-%m-%d', time.localtime(modifytime))
})
# add nonempty values to sn2inforecord
for key, value in [
('serial_number', sn2inforecord.get('sr_no')),
('is_covered', sn2inforecord.get('is_covered')),
('contract_site_customer_name', sn2inforecord.get('contract_site_customer_name')),
('contract_site_address1', sn2inforecord.get('contract_site_address1')),
('contract_site_city', sn2inforecord.get('contract_site_city')),
('contract_site_state_province', sn2inforecord.get('contract_site_state_province')),
('contract_site_country', sn2inforecord.get('contract_site_country')),
('service_line_descr', sn2inforecord.get('service_line_descr')),
('service_contract_number', sn2inforecord.get('service_contract_number')),
('covered_product_line_end_date', sn2inforecord.get('covered_product_line_end_date')),
('parent_sr_no', sn2inforecord.get('parent_sr_no')),
('warranty_type', sn2inforecord.get('warranty_type')),
('warranty_type_description', sn2inforecord.get('warranty_type_description')),
('warranty_end_date', sn2inforecord.get('warranty_end_date')),
]:
value = value.strip()
if value != '' and key not in optional_columns:
sn2info[key] = value
logging.info('create_sn2info_record sn2info: %s' % sn2info)
return sn2info
def inv_cisco_contract(info, params):
set_loglevel()
# list of PIDs to drop
global g_PID_black_list
disable_contract = False
optionalcolumns = ['contract_site_state_province',
'warranty_type_description',
'warranty_end_date',
'warranty_type',
]
base_path = '~/var/ciscoapi'
conf_file = '~/etc/ciscoapi/ciscoapi.conf'
conf_file = os.path.expanduser(conf_file)
# get parameters from wato
if params:
disable_contract = params.get('disable_contract', disable_contract)
optionalcolumns = params.get('removecolumns', optionalcolumns)
g_PID_black_list = list(set(g_PID_black_list + params.get('PID_black_list', g_PID_black_list)))
logging.info('PID_black_list: %s' % g_PID_black_list)
# do nothing, if contract inventory is not explicit enabled via wato
if disable_contract:
return
# check for conf_file and read parameters
if os.path.isfile(conf_file):
configParser = ConfigParser.RawConfigParser()
configParser.read(conf_file)
if configParser.has_option('global', 'base_path'):
base_path = configParser.get('global', 'base_path')
sn2info_dir = base_path + '/sn2info'
path_found = expand_path(sn2info_dir + '/found/')
path_not_found = expand_path(sn2info_dir + '/not_found/')
path_request = expand_path(sn2info_dir + '/request/')
status_path = expand_path(base_path + '/status')
node = inv_tree_list('hardware.support.cisco_contract:')
serials = []
serialscontract = []
logging.info('raw info: %s' % info)
# if no serial or no PID no service ;-)
# remove all empty serials
info = [i for i in info if not check_bad_serial(i[1])]
# remove all empty PIDs
#info = [i for i in info if i[2] != '']
logging.info('info no empty pid/serail: %s' % info)
info = compact_info_by_serial(info)
for phydescr, physerialnum, phymodelname in info:
logging.info('raw pid : %s' % phymodelname)
logging.info('raw sn : %s' % physerialnum)
logging.info('raw desc : %s' % phydescr)
# cut PID/SN at first space sign, change to all Uppercase
pid = phymodelname.split()[0].upper()
physerialnum = physerialnum.split()[0].upper()
# drop all PIDs on Black List, create default node entry for SN
if not pid_on_black_list(pid):
logging.info('PID not on blacklist: %s' % pid)
if phydescr == "":
phydescr = 'requested'
# create list of serial numbers to request contract info for
serials.append(physerialnum)
node.append({
"serial_number" : physerialnum,
"pid" : pid,
"ProductIDDescription": phydescr,
})
logging.info("serial numbers : %s" % serials)
for serial in serials:
logging.info("Serialnumber: %s " % serial)
# find serial in SER-sn2info-covered
if os.path.isfile(path_found + serial):
logging.info("Serial number covered: %s" % serial)
serialscontract.append(create_sn2info_record(path_found + serial, optionalcolumns))
logging.info("Serial number Record: %s" % serialscontract)
# find serial in SER-sn2info-notcovered
elif os.path.isfile(path_not_found + serial):
logging.info("Serial number not covered: %s" % serial)
modifytime = os.path.getmtime(path_not_found + serial)
serialscontract.append({"serial_number": serial,
"is_covered": "NO",
"Last_checked": time.strftime('%Y-%m-%d', time.localtime(modifytime)),
})
else:
# create new request in SER-sn2info-request
logging.info("SER request: %s" % serial)
if not os.path.isfile(path_request + serial):
with open(path_request + serial, 'w+') as f:
pass
modifytime = os.path.getmtime(path_request + serial)
serialscontract.append({"serial_number": serial,
"is_covered": "requested",
"Last_checked": time.strftime('%Y-%m-%d', time.localtime(modifytime)),
})
logging.info("Serials contract : %s" % serialscontract)
# update default entrys with data
for i in node:
# add contract data by serial number
for serial in serialscontract:
if i.get('serial_number') == serial.get('serial_number'):
i.update(serial)
# create and write back api status, will be used for active check cisco_api_status
apistatus = {}
lastrun = {}
apiname = 'sn2info'
use_keys = ['serial_number',
'covered_product_line_end_date',
'service_contract_number',
'Last_checked',
'pid',
'ProductIDDescription',
'Last_checked',
'is_covered',
]
thisrun = []
# add only used keys to status
for entry in node:
this_entry = {}
for key in entry.keys():
if key in use_keys:
this_entry.update({key: entry.get(key)})
thisrun.append(this_entry)
# in CMK v1.5.x global variable g_hostname was replaced by API call host_name()
try:
_hostname = host_name()
except NameError:
_hostname = g_hostname
if os.path.isfile(status_path + _hostname):
with open(status_path + _hostname) as f:
try:
apistatus = json.load(f)
except ValueError, e:
logging.warning('snmp_cisco_contract:status:JSON load error: %s' % e)
if apistatus.get(apiname, None) is not None:
lastrun = apistatus.get(apiname).get('lastrun', {})
if lastrun != thisrun:
apistatus.update({apiname: {}})
apistatus.get(apiname).update({'lastrun': thisrun})
with open(status_path + _hostname, 'w') as f:
json.dump(apistatus, f)
return node
inv_info['inv_cisco_contract'] = {
'inv_function' : inv_cisco_contract,
'snmp_info' : ('.1.3.6.1.2.1.47.1.1.1.1', [ # ENTITY-MIB::entPhysicalEntry
'2', # entPhysicalDescr
'11', # entPhysicalSerialNum
'13', # entPhysicalModelName
]),
'snmp_scan_function': lambda oid: 'cisco' in oid('.1.3.6.1.2.1.1.1.0').lower() or '.1.3.6.1.4.1.9.1' in oid('.1.3.6.1.2.1.1.2.0'),
'includes' : ['ciscoapi.include'],
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment