Skip to content

flashvoid/zabbix-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

General zabbix tools on top of zabbix_api

Developed with aim to bring full power of zabbix_api to command line interface. 
This currently based on gescheit version of api implemetation

https://github.com/gescheit/scripts/blob/master/zabbix/zabbix_api.py
>> 6893ab06cb33acbb3d15159e6f8db2b1cb72551e

zrc		- example config file to placed under $HOME/.zrc or /etc/zrc
z   		- general wrapper
z.complete	- provide completetion service, put under /etc/bash_completion.d

General syntax

z <func> <obj> <--filter|--search|--data> key=val[:key=val...] --show key[,key] [options]


FUNC
	Func is the name of operation you want to preform, avaliable operations
	you can see with 

	z [TAB][TAB]


OBJ
	Obj is the name of object of FUNC, avaliable objects with current FUNC

	z <func> [TAB][TAB]


General filtering
	Depending on selected func you can use --filter or --search or --data
	keyword to define array of parameters for selected FUNC
	You can use [TAB] to get reasonable keys to define for selected FUNC

	EXAMPLE: get details on host with name "zabbix"
	z get host --filter host=zabbix

Options
	There are many of them.
	Most of avaliable options passed directly to zabbix_api. Some useful 
	options described here.

	--search <array>
	Search by any field of item. Take comma separated list of key=val

	--filter <array>
	Filter by any field of item. Take comma separated list of key=val

	--show <list>
	By default script will display output in json tree format. With this
	define comma separated list of fields to output only selected fields.

	--limit <num>
	Select not more than <num> objects.

	-d|--delimeter <char>
	Char that will be used when parsing arrays like --search, filter, or
	--data. Default is comma.
	This need when comma is used in parameter passed to array


EXAMPLES

Example 1
Assume we want to disable "Cron not running" trigger on host "zabbix"
First we need triggerid of this trigger

	bash# z get trigger --filter host=zabbix --search description=Cron \
	--show description,status,value,triggerid
	Return for me:

		status = 0
		description = Cron not running
		value = 1
		triggerid = 7411

status=0 mean trigger enabled and value=1 mean trigger is active
now set status=1 to disable trigger

	bash# z update trigger --data triggerid=7411,status=1
	Return for me:

		{
		  "triggerids": [
		    "7411"
		  ]
		}

Done.


Example 2
Assume now we have bunch of item in the host that has wrong key

	bash# z get item --filter host=hamster --search key_=privmm \
			 --show itemid,key_
	Return for me:

		itemid = 18072
		key_ = vz.monitor[108,privmmpages,limit]

		itemid = 18076
		key_ = vz.monitor[109,privmmpages,limit]

	... and there are many of them

There "privmmpages" must be replaced by "privvmpages"
and it is a bit tricky case we need kep right VEID (108,109 etc.) so i can not 
use massupdate.

First lets get raw itemids to loop them later:

	bash# BADITEM=`z get item --filter host=hamster --search key_=privmm \
		--show itemid,key_ | grep itemid | awk '{ print $3 }' | xargs`
	bash# echo $BADITEM
	Return for me:
		18072 18076 18080 18084 18095 18111 18127 18143 18159 18175 18191 18207 18223 18239 18255 18271
	
Than i define func to get VEID for given itemid

	bash# get_key () { ./z get item --filter host=hamster,itemid=$1 --show key_ | \
				cut -d "[" -f 2 | cut -d "," -f 1 | tail -1; }
	bash# get_key 18072
	Return for me:
		108

Look it working :) cool!
Now func to change wrong key_ to good key_

	bash# set_key () { KEY=`get_key $1`; z update item -d ":" \
		--data itemid=$1:key_=vz.monitor[$KEY,privvmpages,limit]; }
	bash# set_key 18072

There i set up alternate delimeter with -d option, this need to avoid 
conflict when parsing key_ in --data
At this point we must have item updated, let's check

	bash# BADITEM=`z get item --filter host=hamster --search key_=privmm \
                --show itemid,key_ | grep itemid | awk '{ print $3 }' | xargs`
	bash# echo $BADITEM
	Return for me:
                18076 18080 18084 18095 18111 18127 18143 18159 18175 18191 18207 18223 18239 18255 18271
	bash# z get item --filter itemid=18076 --show key_
	Return for me:
		key_ = vz.monitor[109,privvmpages,limit]
	
Item 18072 fixed and it`s not more in my badlist and it`s key is OK now;
Now just loop other in $BADLIST

	bash# for ITEM in $BADLIMITS; do set_key $ITEM; done
	Return for me:
		{
		  "itemids": [
		    "18076"
		  ]
		}
		{
		  "itemids": [
		    "18080"
		  ]
		}
		{
		  "itemids": [
		    "18084"
		  ]
		}
	And so on...

	bash# BADITEM=`z get item --filter host=hamster --search key_=privmm \
                --show itemid,key_ | grep itemid | awk '{ print $3 }' | xargs`
	bash# echo $BADITEM
	Return for me:
	Nohting
	
Job done.

Example 3
Create bunch of item.

Have an items

	bash# z get item --search key_=failcnt --show itemid,key_,hostid --limit 1
	Return for me:
		itemid = 17228
		hostid = 219
		key_ = vz.monitor[149,oomguarpages,failcnt]

All items on one template


	bash# z get template --filter hostid=219 --show host
	Return for me:
		host = hamster_ve

First of all make a list of items for final loop

	bash# ITEMS=`z get item --search key_=failcnt --show itemid | grep itemid \
						| awk '{ print $3 }' | xargs`

Then define two functions, parsing item key to get tokens for description
	
	bash# get_type () { z get item --filter itemid=$1 --show key_ |\
		 tail -1 | cut -d"," -f2 ; }
	bash# get_veid () { z get item --filter itemid=$1 --show key_ |\
		 tail -1 | cut -d"[" -f2 | cut -d"," -f1; }
	bash# get_key () { z get item --filter itemid=$1 key_=failcnt \
		--show itemid,description,key_ | grep key_ | awk '{ print $3 }'; }
	bash# echo "KEY=`get_key 17228`; VEID=`get_veid 17228`; TYPE=`get_type 17228`"
	Return for me:
		KEY=vz.monitor[149,oomguarpages,failcnt]; VEID=149; TYPE=oomguarpages

And one function that actually create trigger

	bash# add_trigger () { KEY=`get_key $1`; VEID=`get_veid $1`; TYPE=`get_type $1`; \
		z create trigger -d "#" --data description=${VEID}_${TYPE}_fail#\
		expression="{hamster_ve:${KEY}.abschange(0)} > 0"#\
		status=0#\
		priority=4; }

Two moments here. First, # is used as delimeter to avoid conflict with
expression. Second, spaces in expression are important. In web you can 
define expression like this 

	"{hamster_ve:${KEY}.abschange(0)}>0"
and it will work, but via API there are  ^^^ must be spaces

So now just loop all items

	bash# for ITEM in $ITEMS; do add_trigger $ITEM; done

Done.


TODO
	- Not supported usermacro for now in case of outdated api.
	- Not supported discovery
	- Not supported mass-update, use bash cickles

About

various tools on top of python api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published