Raymond Kuiper - Working the API like a Unix Pro

Zabbix 3,256 views 35 slides Sep 16, 2015
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Communicating with the Zabbix API can be quite cumbersome, especially if you don't have a background as a programmer. For a sysadmin, it would be very nice if one could just run some CLI commands to control Zabbix behavior.

Wouldn't it be wonderful if you could fetch a list of active trigge...


Slide Content

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
ZabConf2015 – Sept 11 2015
---
Raymond Kuiper
Working the API like a Unix Pro

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Quick introduction
●@RaymondKuiper
●Zabbix user since 2006, never looked back
●q1x on Freenode (fnd me in #zabbix)
●Proud to work for these guys:

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Sysadmins <3 Automation

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Automation in Zabbix

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
How to automate via the API?

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
API bash script using Curl
Pros:
●Seems nice and easy at frst
●You can show of your mad curl skills

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
API bash script using Curl
Cons:
●No object awareness
●Honestly, it's a PITA

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Develop an API based tool
Pros:
●Perfect solution to your problem
●Highly efcient code

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Develop an API based tool
Cons:
●Takes time and efort to develop (and
debug) a ftting solution
●Not very fexible unless you invest heavily
in development (monolithic)
●Gets very complex, very fast. You might
need a developer to maintain it

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Use a CLI tool
(Zabcon and friends)
Pros:
●Can be used in shell scripts
●Can be used quickly, no need for direct
API code

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Use a CLI tool
(Zabcon and friends)
Cons:
●Learning a new CLI language
●Limited to the functionality the developer
thought was useful

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
My solution?

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
*nix
The IT swiss army knife

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
*nix
The IT swiss army knife
“The whole is greater than the sum of its
parts.”
― Aristotle

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Introducing:
the Zabbix Gnomes

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Zabbix Gnomes
https://github.com/q1x/zabbix-gnomes

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Zabbix Gnomes
~/.zbx.conf
[Zabbix API]
username=johndoe
password=verysecretpassword
api=https://zabbix.mycompany.com/path/to/zabbix/frontend/
no_verify=true

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Zabbix Gnomes
Making them work together

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 1: Hostgroup Templates
Scenario:
●Hosts in the host group 'App Servers' should
be linked to 'Template App Customapp'
●$someone keeps forgetting to link that
template to newly deployed App servers
●Post-it notes about this don't seem to work

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 1: Hostgroup Templates
user@localhost$ ./zghostfinder.py 'App Servers'
app-server-001
app-server-002
...
app-server-042
app-server-043
(Finds hosts in a host group)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 1: Hostgroup Templates
user@localhost$ ./zhtmplfinder.py 'app-server-001'
Template OS Linux
Template App Customapp
user@localhost$ ./zhtmplfinder.py 'app-server-042'
Template OS Linux
(Finds templates linked to a host)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 1: Hostgroup Templates
user@localhost$ ./zhtmpllinker.py
usage: zhtmpllinker.py [-h]
(-H HOSTNAMES [HOSTNAMES ...] | -G
HOSTGROUPS [HOSTGROUPS ...])
-t TEMPLATES [TEMPLATES ...]
user@localhost$ ./zhtmpllinker.py -G 'App Servers' \
-t 'Template App Customapp'
(Links templates to hosts)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 1: Hostgroup Templates
user@localhost$ ./zhtmpllinker.py -G 'App Servers' \
-t 'Template App Customapp'
*Pro-tip: Stick it in a crontab!*

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 1: Hostgroup Templates
user@localhost$ for host in $(./zghostfinder.py 'Netco Test');
do echo "$host : $(./zhtmplfinder.py "$host" | tr '\n' ',')";
done
app-server-001 : Template OS Linux,Template App
app-server-002 : Template OS Linux,Template App
...
app-server-042 : Template OS Linux,Template App
app-server-043 : Template OS Linux,Template App

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 2: Host Inventory
Scenario:
●$bossman has negotiated a new hardware
support contract with $vendor
●Demands serial/os/type of all Cisco 800s
●Your template stores this info in the Inventory
●You are a happy user of network discovery

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 2: Host Inventory
user@localhost$ ./zhinvswitcher.py
usage: zhinvswitcher.py [-h] (-H HOSTNAMES [HOSTNAMES ...]
| -G HOSTGROUPS [HOSTGROUPS ...]
| --all-hosts) … [-m MODE] …
(Switches Inv. Mode on hosts)
user@localhost$ ./zhinvswitcher.py -G “Cisco Devices” -m autouser@localhost$ ./zhinvswitcher.py -G “Cisco Devices” -m auto
(See also ZBXNEXT-1241)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 2: Host Inventory
user@localhost$ ./zgetinventory.py
usage: zgetinventory.py [-h] (-H HOSTNAMES [HOSTNAMES ...]
| -G HOSTGROUPS [HOSTGROUPS …]) …
[-m] [-i] … (-A | -F FIELDS [FIELDS ...])
(Gets Zabbix inventory information)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 2: Host Inventory
user@localhost$ ./zgetinventory.py -G 'Cisco Devices ' \
-e -m -i -F serialno_a type os | \
sed -n -e 1p -e '/C8../'p | \
cut -d ',' -f 2-
"host","serialno_a","type","os"
"RT01","FCZ999999A","C819G-4G-G-K9","15.4(3)M1, RELEASE SOFTWARE
(fc1)"
"RT02","FCZ999999B","C887VA-K9","15.4(1)T1, RELEASE SOFTWARE
(fc2)"
"RT03","FCZ999999C","C887VA-K9","15.4(3)M1, RELEASE SOFTWARE
(fc1)"
...

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 3: Emailing Graphs
Scenario:
●$colleague needs daily CPU graphs
●Can't be bothered with browsing the GUI
every morning before getting cofee
●You'd like to help him reach his mailbox quota

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 3: Emailing Graphs
user@localhost$ ./zghostfinder.py 'Linux Servers'
Wakizashi
Kodachi
Katana

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 3: Emailing Graphs
user@localhost$ ./zhgraphfinder.py -e Kodachi
568:Network traffic on eth0
555:CPU jumps
556:CPU load
557:CPU utilization
558:Swap usage
569:Disk space usage /
560:Memory usage
(Finds graphs confgured on a host)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Ex. 3: Emailing Graphs
user@localhost$ ./zgetgraph.py -f graph.png 557
(Downloads a graph from the frontend)

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
#!/bin/bash
graphs=""
filelist=""
# find graphs on hosts in group 'Linux Servers'
for host in $(zghostfinder.py "Linux Servers"); do
hgraphs=$(zhgraphfinder.py -e $host | grep "CPU utilization" \
| cut -d ':' -f 1 )
graphs="$graphs $hgraphs"
done
# download graphs
for graph in $graphs; do
file="/tmp/$graph.png"
filelist="$filelist -a $file"
zgetgraph.py -t 86400 -f "$file" "$graph"
done
# Mail the graphs and cleanup
echo "See attached." | mail $filelist -s "Daily graphs"
"[email protected]"
rm -r $(echo "$filelist" | sed 's/-a\ //g')
Ex. 3: Emailing Graphs

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Things to think about

L
a
y
o
u
t

b
y

o
r
n
g
j
c
e
2
2
3
,

C
C
-
B
Y
Questions?