Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added -n option to find host by ID #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions zhostfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ def ConfigSectionMap(section):
no_verify=true

""")
group = parser.add_mutually_exclusive_group(required=True)

group=parser.add_argument_group("Query Options - can be combined together")

group.add_argument('-S', '--search', help='Hostname string to find in Zabbix')
group.add_argument('-A', '--all', help='Returns all hosts Zabbix',action='store_true')
group.add_argument('-I', '--ip', help='IP to find in zabbix')
group.add_argument('-G', '--group', help='Group to find in zabbix')
group.add_argument('-D', '--dnsname', help='hostname to find in zabbix')


parser.add_argument('-u', '--username', help='User for the Zabbix api')
parser.add_argument('-p', '--password', help='Password for the Zabbix api user')
parser.add_argument('-a', '--api', help='Zabbix API URL')
Expand Down Expand Up @@ -119,22 +126,34 @@ def ConfigSectionMap(section):

# Find the hostgroup we are looking for
search_name = args.search

if search_name:
# Find matching hosts
if args.monitored:
hosts = zapi.host.get(output="extend", monitored_hosts=True, search={"host":search_name})
else:
hosts = zapi.host.get(output="extend", search={"host":search_name})
elif args.all:
# Find matching hosts
if args.monitored:
hosts = zapi.host.get(output="extend", monitored_hosts=True)
else:
hosts = zapi.host.get(output="extend")
else:
sys.exit("Error: No hosts to find")

search_ip = args.ip
search_group = args.group
search_dns_name = args.dnsname

if not search_name and not search_ip and not search_group and not search_dns_name and not args.all:
sys.exit("Error: No hosts to find")


# Find matching hosts
group_id=""

if search_group:
group = zapi.hostgroup.get(output="extend", filter=({'name': search_group}))
if group:
group_id = group[0]["groupid"]
else:
sys.exit("Error: Could not find any group \""+ search_group + "\"")

queryDict={"search": {"host":search_name, "ip":search_ip, "group":search_group, "dns":search_dns_name}}

if group_id:
queryDict["groupids"]=group_id
if args.monitored:
queryDict["monitored_hosts"]=args.monitored

hosts = zapi.host.get(output="extend", **queryDict)


if hosts:
if args.extended:
# print ids and names
Expand All @@ -150,6 +169,10 @@ def ConfigSectionMap(section):
for host in hosts:
print(format(host["host"]))
else:
sys.exit("Error: Could not find any hosts matching \""+ search_name + "\"")
if search_name:
sys.exit("Error: Could not find any hosts matching name \""+ search_name + "\"")
elif search_ip:
sys.exit("Error: Could not find any hosts matching ip \""+ search_ip + "\"")


# And we're done...
7 changes: 6 additions & 1 deletion zhostupdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def ConfigSectionMap(section):
parser.add_argument('--no-verify', help='Disables certificate validation when using a secure connection',action='store_true')
parser.add_argument('-c','--config', help='Config file location (defaults to $HOME/.zbx.conf)')
parser.add_argument('-N', '--name', help='Update hostname')
parser.add_argument('-n', '--numeric', help='Provide a host ID instead of a name', action='store_true')
group.add_argument('-V', '--visible-name', help='Update visible name')
group.add_argument('-S', '--sync-names', help='Sets hostname and visible name to the name specified with -N',action='store_true')
parser.add_argument('-I', '--inventory', help='Update inventory fields. Specify each field as \'fieldname="value"\'.', nargs='+')
Expand Down Expand Up @@ -133,8 +134,12 @@ def ConfigSectionMap(section):
call={}

if host_name:
if args.numeric:
filterField = "hostid"
else:
filterField = "host"
# Find matching hosts
hosts = zapi.host.get(output="extend", selectGroups="extend", selectMacros="extend", filter={"host":host_name})
hosts = zapi.host.get(output="extend", selectGroups="extend", selectMacros="extend", filter={filterField:host_name})
if hosts:
# Basic API call params
call["hostid"]=hosts[0]["hostid"]
Expand Down