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 242d5110 authored by thl-cmk's avatar thl-cmk :flag_na:
Browse files

Delete ciscoapi.py

parent 554840df
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# License: GNU General Public License v2
#
# Author: thl-cmk[at]outlook[dot]com
# URL : https://thl-cmk.hopto.org
# Date : 2017-03-20
#
# include file, will be used with snmp_cisco_eox and snmp_cisco_contract
#
# 2017-05-29: fixed empty pid handling
# added serial number cleanup
# 2021-07-23: rewrite for CMK 2.0
#
import os
import logging
import re
import json
from typing import List
#
# global variables
#
# list of PIDs to drop
g_PID_black_list = ['BUILT-IN', 'MICRON', 'C400-MTFDD']
# list of PIDs to try by serial number
g_PID_bad_list = ['UNSPECIFIED', 'FABRIC', 'ASA', 'C2611XM-2FE', 'FTLX8570D3BCL', 'FTLF8519P2BCL', 'FTLX8571D3BCL',
'FTRJ-8519-7D', 'PLRXPL-SC-S43'] #
# list of S/Ns to drop
g_SN_black_list = []
def get_base_path() -> str:
conf_file = os.path.expanduser('~/etc/ciscoapi/ciscoapi.json')
base_path = '~/var/ciscoapi'
# check for conf_file and read parameters
if os.path.isfile(conf_file):
with open(conf_file) as f:
try:
config = json.load(f)
base_path = config['global'].get('base_dir', base_path)
except ValueError as e:
logging.warning(f'inv_cisco_contract:status:JSON load error: {e}')
return base_path
def set_pid_black_list(pid_black_list: List[str]):
global g_PID_black_list
if pid_black_list:
g_PID_black_list = list(set(g_PID_black_list + pid_black_list))
def set_pid_bad_list(pid_bad_list: List[str]):
global g_PID_bad_list
if pid_bad_list:
g_PID_bad_list = list(set(g_PID_bad_list + pid_bad_list))
def set_sn_black_list(sn_black_list: List[str]):
global g_SN_black_list
if sn_black_list:
g_SN_black_list = list(set(g_SN_black_list + sn_black_list))
# check if dir exists, if not try to create it.
# return True if dir exists or creation was ok.
# return False if dir not exists and creation was not ok
def check_dir_and_create(directory):
directory = os.path.dirname(directory)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except:
return False
return True
# expand user dir and add '/' if necessary and create directory if it not exists
def expand_path(path):
homedir = os.path.expanduser('~')
if path.startswith('~'):
path = homedir + path[1:]
if not path.endswith('/'):
path += '/'
if not check_dir_and_create(path):
return ''
return path
# returns True if SN on black list
def sn_on_black_list(serial):
global g_SN_black_list
if serial.upper() in g_SN_black_list:
return True
return False
# returns True if PID on black list
def pid_on_black_list(pid):
global g_PID_black_list
for drop_PID in g_PID_black_list:
if pid.startswith(drop_PID.upper()):
return True
# if PID not on Black list return false
return False
# returns True if PID on Bad list
def pid_on_bad_list(pid):
global g_PID_bad_list
# remove all chars from string, except allowedchars
allowedchars = re.compile('[^a-zA-Z0-9_=\/\-\+\.\\\]')
cleanpid = allowedchars.sub('', pid).strip()
# if PID contains illegal signs or if pid empty try by serial number
if (cleanpid != pid) or (cleanpid == ''):
return True
# list of bad PIDs :-( we need to try get EoX info via serial number for this PIDs
# needs to be configurable via a file if too big for wato ;-(
for bad_PID in g_PID_bad_list:
if pid.startswith(bad_PID):
return True
return False
# returns True if bad serial
def check_bad_serial(serial):
logging.info('Check_bad_serial:serial: %s' % serial)
serial = serial.replace(' ', '')
# remove all chars from string, except allowedchars
allowedchars = re.compile('[^a-zA-Z0-9_=\-\+\.\\\]')
cleanserial = allowedchars.sub('', serial).strip()
logging.info('Check_bad_serial:cleanserial: %s' % cleanserial)
# if serial contains illegal signs or empty return true
if (cleanserial != serial) or (cleanserial == ''):
logging.info('Check_bad_serial:bad:serial is bad')
return True
logging.info('Check_bad_serial:serial is god')
return False
def set_loglevel():
# set default logglevel
logging.basicConfig(level=logging.WARNING)
# has no effect as long previous command is active (is by design)
logging.basicConfig(level=logging.INFO)
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