Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_host_get(self):
httpretty.register_uri(
httpretty.POST,
"http://example.com/api_jsonrpc.php",
body=json.dumps({
"jsonrpc": "2.0",
"result": [{"hostid": 1234}],
"id": 0
}),
)
zapi = ZabbixAPI('http://example.com')
zapi.auth = "123"
result = zapi.host.get()
# Check request
self.assertEqual(
json.loads(httpretty.last_request().body.decode('utf-8')),
{
'jsonrpc': '2.0',
'method': 'host.get',
'params': {},
'auth': '123',
'id': 0,
}
)
# Check response
if args.no_verify:
noverify = args.no_verify
# test for needed params
if not username:
sys.exit("Error: API User not set")
if not password:
sys.exit("Error: API Password not set")
if not api:
sys.exit("Error: API URL is not set")
# Setup Zabbix API connection
zapi = ZabbixAPI(api)
if noverify is True:
zapi.session.verify = False
# Login to the Zabbix API
zapi.login(username, password)
##################################
# Start actual API logic
##################################
if args.all_hosts:
# Make a list of all hosts
hlookup = zapi.host.get()
else:
if args.hostgroups:
if args.no_verify:
noverify = args.no_verify
# test for needed params
if not username:
sys.exit("Error: API User not set")
if not password:
sys.exit("Error: API Password not set")
if not api:
sys.exit("Error: API URL is not set")
# Setup Zabbix API connection
zapi = ZabbixAPI(api)
if noverify is True:
zapi.session.verify = False
# Login to the Zabbix API
zapi.login(username, password)
##################################
# Start actual API logic
##################################
if not args.triggerid:
sys.exit("Error: Triggerid not found")
if args.enable:
if args.no_verify:
noverify = args.no_verify
# test for needed params
if not username:
sys.exit("Error: API User not set")
if not password:
sys.exit("Error: API Password not set")
if not api:
sys.exit("Error: API URL is not set")
# Setup Zabbix API connection
zapi = ZabbixAPI(api)
if noverify is True:
zapi.session.verify = False
# Login to the Zabbix API
zapi.login(username, password)
##################################
# Start actual API logic
##################################
# Find the template we are looking for
tmpl_name = args.template
template = zapi.template.get(output="extend", filter={"host": tmpl_name})
templateid = template[0]["templateid"]
# test for needed params
if not username:
print("Error: API User not set")
exit()
if not password:
print("Error: API Password not set")
exit()
if not api:
print("Error: API URL is not set")
exit()
# Setup Zabbix API connection
zapi = ZabbixAPI(api)
if noverify is True:
zapi.session.verify = False
# Login to the Zabbix API
zapi.login(username, password)
##################################
# Start actual API logic
##################################
# set the hostname we are looking for
host_name = args.hostname
# Find specified host from API
hosts = zapi.host.get(output="extend", filter={"host": host_name})
# OTHER VARIABLES
args = parser.parse_args()
fake = args.fake
LOGFILE = '/tmp/zbx_discovery-manager.log'
logger = LogPrint(echo=args.verbose, logfile=LOGFILE, loglevel=args.loglevel.upper())
loglevels = {
'CRITICAL' : 50,
'ERROR' : 40,
'WARNING' : 30,
'INFO' : 20,
'DEBUG' : 10
}
try:
zapi = ZabbixAPI(args.url,timeout=TIMEOUT)
zapi.login(args.user,args.password)
except Exception, e:
logger.error('Unable to login: {0}'.format(e))
exit(1)
# CUSTOM FUNCTIONS
def discovery_checks():
"""
retornar o formato json com os discovery checks
essa entrada eh manual
"""
dchecks = [
{ 'uniq': '1', 'snmp_community': '{$SNMP_COMMUNITY}', 'type': '11', 'ports': '161', 'key_': 'sysName.0' },
{ 'uniq': '0', 'snmp_community': '{$SNMP_COMMUNITY}', 'type': '11', 'ports': '161', 'key_': 'sysDescr.0' },
{ 'uniq': '0', 'snmp_community': '{$SNMP_COMMUNITY}', 'type': '11', 'ports': '161', 'key_': 'sysContact.0' },
{ 'uniq': '0', 'snmp_community': '{$SNMP_COMMUNITY}', 'type': '11', 'ports': '161', 'key_': 'MIB-Dell-10892::chassisModelName.1' },
Return ZabbixAPI object
"""
# pyzabbix library, with user\password in login method. It's GOOD library
logging.debug("Try connect to Zabbix by pyzabbix...")
try:
zbx_pyzabbix = ZabbixAPI(zbx_url)
zbx_pyzabbix.session.verify = False
zbx_pyzabbix.login(zbx_user, zbx_password)
return zbx_pyzabbix
except Exception as e:
logging.exception(e)
# py-zabbix library, with user\password in ZabbixAPI
logging.debug("Try connect to Zabbix by py-zabbix...")
try:
zbx_py_zabbix = ZabbixAPI(zbx_url, user=zbx_user, password=zbx_password)
zbx_py_zabbix.session.verify = False
return zbx_py_zabbix
except Exception as e:
logging.exception(e)
# choose good API
raise Exception("Some error in pyzabbix or py_zabbix module, see logs")
def main(args):
zapi = pyzabbix.ZabbixAPI('http://%s' % args.zabbix_server)
zapi.login(args.zabbix_username, args.zabbix_password)
for host in args.host:
process(zapi, host)
'''
Created on 01.10.2010
@author: gescheit
'''
from pyzabbix import ZabbixAPI
server="127.0.0.1"
username="api"
password="apipass"
zapi = ZabbixAPI(server=server, path="", log_level=6)
zapi.login(username, password)
host_name="test_host"
hostid=zapi.host.get(filter={"host":host_name})[0]["hostid"]
print hostid
zapi.item.create(hostid=hostid,
description='Used disk space on $1 in %' ,
key_='vfs.fs.size[/,pused]',
)