Есть известный скрипт cisco-psec-traphandler.py , с помощью которого производится обработка "сырого" сообщения с устройства cisco. Вот его начало.
Не могу понять, откуда берется вводная часть для обработки
# now parse the stdin
inp = sys.stdin
Это вроде как стандартный ввод , по-умолчанию, с клавиатуры. Но мы же не с клавиатуры получаем материал, а как говорит теория, согласно настройке config.ini парится лог
logfile = /var/log/cisco-errdisable-traphandler.log
Но несмотря на то, что из сети идет много трапов, время последнего изменения этого файла - несколько дней назад.
Объясните, пожалуйста, что за ввод со стандартного ввода имеется здесь в виду?
PHP Code:
import sys, os, re, subprocess
import logging.handlers, shlex
from pysnmp.hlapi import *
from configparser import ConfigParser
from ipaddress import IPv4Address, AddressValueError
from pyzabbix import ZabbixAPI
def read_config(filename, section):
""" Read a configuration file and return a dictionary object
aram filename: name of the configuration file
aram section: section of a configuration
:return: a dictionary of a configfile section parameters
"""
# create parser and read ini configuration file
parser = ConfigParser()
parser.read(filename)
# get section
db = {}
if parser.has_section(section):
items = parser.items(section)
for item in items:
db[item[0]] = item[1]
else:
raise Exception('{0} not found in the {1} file'.format(section, filename))
return db
def find_ifDesc_from_ifIndex(hostname, ifIndex, community="public"):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(community),
UdpTransportTarget((hostname, 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr', ifIndex)))
)
if errorIndication:
logging.error(errorIndication)
elif errorStatus:
logging.error('%s at %s', errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')
exit(1)
else:
ifDescr = varBinds[0][1].prettyPrint()
return ifDescr
# find the path where the script is located to find a config.ini nearby
dir_path = os.path.dirname(os.path.realpath(__file__))
conf_file_name = dir_path + '/config.ini'
# fetch config parameters
snmp_config = read_config(conf_file_name, section = 'snmp')
api_config = read_config(conf_file_name, section = 'api')
logging_config = read_config(conf_file_name, section = 'logging')
zabbix_config = read_config(conf_file_name, section='zabbix')
#db_config = read_config(section = 'mysql')
# set logging handler
handler = logging.handlers.WatchedFileHandler(os.environ.get ("LOGFILE", logging_config['logfile']))
#formatter = logging.Formatter(logging.BASIC_FORMAT)
formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", logging_config['loglevel']))
root.addHandler(handler)
# now parse the stdin
inp = sys.stdin
# the first line is alwats hostname
hostname = inp.readline().strip()
# the second line contains an IP address that should be fetched by regexp
trapstr = inp.readline()
m = re.search("\[(.*)\]:[0-9]+->", trapstr)
ip = m.group(1)
Не могу понять, откуда берется вводная часть для обработки
# now parse the stdin
inp = sys.stdin
Это вроде как стандартный ввод , по-умолчанию, с клавиатуры. Но мы же не с клавиатуры получаем материал, а как говорит теория, согласно настройке config.ini парится лог
logfile = /var/log/cisco-errdisable-traphandler.log
Но несмотря на то, что из сети идет много трапов, время последнего изменения этого файла - несколько дней назад.
Объясните, пожалуйста, что за ввод со стандартного ввода имеется здесь в виду?
Comment