├── Makefile ├── README.md ├── examples ├── README.md ├── export_all_templates ├── export_template_by_name ├── host.get ├── host_macro └── template.get ├── test ├── empty_zabbix_api_call └── export_template_by_name ├── zapish ├── zapish.1 ├── zapish.1.xml ├── zapish.inc ├── zapish.inc.3 └── zapish.inc.3.xml /Makefile: -------------------------------------------------------------------------------- 1 | VERSION = 0.99 2 | 3 | libexecdir = /usr/libexec 4 | pkglibexecdir = ${libexecdir}/zapish 5 | mandir = /usr/share/man 6 | man1dir = ${mandir}/man1 7 | man3dir = ${mandir}/man3 8 | 9 | MKDIR_P = mkdir -p 10 | DESTDIR = 11 | INSTALL = /usr/bin/install -c 12 | INSTALL_DATA = ${INSTALL} -m 644 13 | INSTALL_SCRIPT = ${INSTALL} 14 | 15 | all: zapish.inc.3 zapish.1 16 | 17 | zapish.inc.3: zapish.inc.3.xml 18 | xsltproc \ 19 | --param "man.authors.section.enabled" "1" \ 20 | --stringparam "man.output.base.dir" "0" \ 21 | -nonet http://docbook.sourceforge.net/release/xsl/current/manpages/profile-docbook.xsl $< 22 | 23 | zapish.1: zapish.1.xml 24 | xsltproc \ 25 | --param "man.authors.section.enabled" "1" \ 26 | --stringparam "man.output.base.dir" "0" \ 27 | -nonet http://docbook.sourceforge.net/release/xsl/current/manpages/profile-docbook.xsl $< 28 | 29 | install: 30 | $(MKDIR_P) "$(DESTDIR)$(man3dir)" "$(DESTDIR)$(pkglibexecdir)" 31 | $(INSTALL_DATA) zapish.inc.1 "$(DESTDIR)$(man1dir)" 32 | $(INSTALL_DATA) zapish.inc.3 "$(DESTDIR)$(man3dir)" 33 | $(INSTALL_DATA) zapish.inc "$(DESTDIR)$(pkglibexecdir)" 34 | 35 | dist: 36 | 37 | 38 | rpm: 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## zapish - Zabbix API SHell binding 2 | ----------------------------------- 3 | 4 | The zapish.inc is the file to include in own SH/bash scripts with few functions which make calling Zabbix API easy. 5 | 6 | It consists of few functions for: 7 | * construct JSON string fields like string, numeric and arrays 8 | * the main zabbix_api function which is used to call Zabbix API 9 | * JSON return values parser/extractor 10 | * the zapish_init function which reads ~/.zapish.rc configuration file or initialize if it does not exist 11 | 12 | --- 13 | At the moment zapish.inc is not fully POSIX SH compliant because it uses in few places "local" keyword to define functions local variables. However, the 14 | intention is to rewrite it to make it fully POSIX SH compliant code. 15 | 16 | Version: (still released not yet) v1.0 rc1 17 | 18 | Known bugs: 19 | * None 20 | 21 | ToDo: 22 | * replace jq by JSON parser written in SH 23 | * add function to convert regular xml to shell quoted format used configure.import zabbix API 24 | * finish zapish.inc.3.xml documentation 25 | * more test units and examples 26 | 27 | ------------------------------------ 28 | #### License ## 29 | The GNU General Public License Version 2. 30 | 31 | #### Copyright (C) 2017-2018 Tomasz Kłoczko 32 | 33 | ------------------------------------ 34 | Note: "zapish" pronunced in English sounds like Polish "zapisz" which means "write it down!" (as commanding) 35 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | List of example scripts: 2 | - export_all_templates - dump all templates registered in the zabbix to the separated formated xml files 3 | - export_template_by_name - dump single template to xml formated file by name of the template 4 | - template.get - example which resolves template name to templateid 5 | -------------------------------------------------------------------------------- /examples/export_all_templates: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e ../zapish.inc ]; then 4 | . ../zapish.inc 5 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 6 | . /usr/libexec/zapish/zapish.inc 7 | else 8 | echo "No zapish.inc found" 9 | exit 1 10 | fi 11 | 12 | # Get all templates IDs 13 | json_get "$(zabbix_api template.get \ 14 | "$(json_list params \ 15 | "$(json_str output extend \ 16 | "")" \ 17 | "")" \ 18 | )" '.result[].name' | \ 19 | while read template_name; do 20 | # strip down leading and trailing quotes 21 | template_name="${template_name:1:-1}" 22 | 23 | template_id="$(json_get "$(zabbix_api template.get \ 24 | "$(json_list params \ 25 | "$(json_list "filter" \ 26 | "$(json_str "name" \ 27 | "${template_name}" \ 28 | "")" \ 29 | "")" \ 30 | "$(json_str "output" "simple" \ 31 | "")" \ 32 | "")" \ 33 | )" '.result[].templateid')" 34 | 35 | xml_output="$(zabbix_api configuration.export \ 36 | "$(json_list params \ 37 | "$(json_list options \ 38 | "$(json_array_num templates \ 39 | ${template_id} \ 40 | "")" \ 41 | "")" \ 42 | "$(json_str format xml \ 43 | "")" \ 44 | "")" \ 45 | )" 46 | 47 | printf "Template id=%7s, Name=%-40s dump to \"%s.xml\"\n" ${template_id} "\"${template_name}\"" "${template_name}" 48 | echo "${xml_output}" | jq -r .result | xmllint --format - > "${template_name}.xml" 49 | #printf "Template id=%7s, Name=%-40s dump to \"%s.raw\"\n" ${template_id} "\"${template_name}\"" "${template_name}" 50 | #echo "${xml_output}" | jq .result > "${template_name}.raw" 51 | done 52 | -------------------------------------------------------------------------------- /examples/export_template_by_name: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e ../zapish.inc ]; then 4 | . ../zapish.inc 5 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 6 | . /usr/libexec/zapish/zapish.inc 7 | else 8 | echo "No zapish.inc found" 9 | exit 1 10 | fi 11 | 12 | template_name="ICMP" 13 | 14 | templates=$(zabbix_api template.get \ 15 | "$(json_list params \ 16 | "$(json_str output simple \ 17 | "")" \ 18 | "$(json_list filter \ 19 | "$(json_array_str "name" \ 20 | "${template_name}" \ 21 | "")" \ 22 | "")" \ 23 | "")" \ 24 | ) 25 | 26 | templateid="$(json_get "${templates=}" '.result[].templateid')" 27 | 28 | xml_output=$(zabbix_api configuration.export \ 29 | "$(json_list params \ 30 | "$(json_list options \ 31 | "$(json_array_num templates \ 32 | ${templateid} \ 33 | "")" \ 34 | "")" \ 35 | "$(json_str format xml \ 36 | "")" \ 37 | "")" \ 38 | ) 39 | 40 | echo "Template: id=${templateid}, Name=\"${template_name}\" dump to ${template_name}.xml and ${template_name}.raw" 41 | echo ${xml_output} | jq -r .result | xmllint --format - > ${template_name}.xml 42 | echo ${xml_output} | jq .result > ${template_name}.raw 43 | -------------------------------------------------------------------------------- /examples/host.get: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # get template by name 4 | 5 | if [ -e ../zapish.inc ]; then 6 | . ../zapish.inc 7 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 8 | . /usr/libexec/zapish/zapish.inc 9 | else 10 | echo "No zapish.inc found" 11 | exit 1 12 | fi 13 | 14 | print_help() { 15 | echo "Usage: host.get [OPTION] " 16 | echo 17 | echo " -h, --help Print this help" 18 | echo 19 | } 20 | 21 | 22 | if [ $# = 0 ]; then 23 | print_help 24 | exit 1 25 | fi 26 | 27 | while [ $# -gt 0 ]; do 28 | cmd="${1}" 29 | shift 30 | case "${cmd}" in 31 | "-h" | "--help" ) 32 | print_help 33 | ;; 34 | * ) 35 | hostname="$cmd" 36 | zapish_result=$(zabbix_api host.get \ 37 | "$(json_list params \ 38 | "$(json_list "filter" \ 39 | "$(json_array_str "name" "$hostname" \ 40 | "" \ 41 | "")" \ 42 | "")" \ 43 | "$(json_str "output" "simple" \ 44 | "")" \ 45 | "")" \ 46 | ) 47 | ;; 48 | esac 49 | done 50 | 51 | echo "hostname \"$hostname\" hostid is $(json_get "${zapish_result}" '.result[].hostid')" 52 | -------------------------------------------------------------------------------- /examples/host_macro: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # get template by name 4 | 5 | if [ -e ../zapish.inc ]; then 6 | . ../zapish.inc 7 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 8 | . /usr/libexec/zapish/zapish.inc 9 | else 10 | echo "No zapish.inc found" 11 | exit 1 12 | fi 13 | 14 | print_help() { 15 | echo "Usage: host_macro [] []" 16 | echo 17 | echo "Commands:" 18 | echo " create create host macro" 19 | echo " get list host macros" 20 | echo " help print this help" 21 | echo " delete delete host macro" 22 | echo " update update existing host macro" 23 | echo 24 | exit 1 25 | } 26 | 27 | if [ $# = 0 ]; then 28 | print_help 29 | fi 30 | 31 | get_host_name() { 32 | if [ -z "${1}" ]; then 33 | echo 34 | echo "Error: missing host name specified" 35 | echo 36 | print_help 37 | fi 38 | host_name="${1}" 39 | } 40 | 41 | get_macro_name() { 42 | if [ -z "${1}" ]; then 43 | echo 44 | echo "Error: missing macro name" 45 | echo 46 | print_help 47 | fi 48 | macro_name="${1}" 49 | } 50 | 51 | get_macro_value() { 52 | if [ -z "${1}" ]; then 53 | echo 54 | echo "Error: missing macro name" 55 | echo 56 | print_help 57 | fi 58 | macro_value="$(echo ${1} | sed 's,\",\\\",g')" 59 | } 60 | 61 | get_macro_id() { 62 | if [ -z "${1}" ]; then 63 | echo 64 | echo "Error: missing macroid to delete" 65 | echo 66 | print_help 67 | fi 68 | macro_id="${1}" 69 | } 70 | 71 | 72 | command="${1}" 73 | shift 74 | 75 | case "${command}" in 76 | "get") 77 | get_host_name "${1}" 78 | ;; 79 | "create") 80 | get_host_name "${1}" 81 | shift 82 | get_macro_name "${1}" 83 | shift 84 | get_macro_value "${1}" 85 | ;; 86 | "delete") 87 | get_host_name "${1}" 88 | shift 89 | get_macro_name "${1}" 90 | ;; 91 | "update") 92 | get_macro_id "${1}" 93 | shift 94 | get_macro_value "${1}" 95 | ;; 96 | 'help') 97 | print_help 98 | ;; 99 | *) 100 | print_help 101 | ;; 102 | esac 103 | 104 | histid_str="$(json_get "$(zabbix_api host.get \ 105 | "$(json_list params \ 106 | "$(json_list "filter" \ 107 | "$(json_array_str "name" "${host_name}" \ 108 | "" \ 109 | "")" \ 110 | "")" \ 111 | "$(json_str "output" "simple" \ 112 | "")" \ 113 | "")" \ 114 | )" '.result[].hostid')" 115 | 116 | hostid="${histid_str:1:-1}" 117 | 118 | if [ -z "${hostid}" ]; then 119 | echo 120 | echo "Hostname \"${host_name}\" does not exis" 121 | echo 122 | print_help 123 | fi 124 | 125 | case "${command}" in 126 | "get") 127 | echo "Hostname: \"${host_name}\"" 128 | result="$(zabbix_api usermacro.get \ 129 | "$(json_list "params" \ 130 | "$(json_str "output" "extend")" \ 131 | "$(json_str "hostids" "${macro_id}")" \ 132 | "")" \ 133 | "")" 134 | echo "${result}" | jq . 135 | ;; 136 | "create") 137 | result="$(zabbix_api usermacro.create \ 138 | "$(json_list "params" \ 139 | "$(json_str "hostid" "${hostid}")" \ 140 | "$(json_str "macro" "${macro_name}")" \ 141 | "$(json_str "value" "${macro_value}")" \ 142 | "")" 143 | "")" 144 | echo "${result}" | jq . 145 | ;; 146 | "delete") 147 | result="$(zabbix_api usermacro.delete \ 148 | "$(json_array_str "params" "${macro_name}" "" \ 149 | "")" 150 | "")" 151 | echo "${result}" | jq . 152 | ;; 153 | "update") 154 | result="$(zabbix_api usermacro.update \ 155 | "$(json_list "params" \ 156 | "$(json_str "macro" "${macro_id}")" \ 157 | "$(json_str "value" "${macro_value}")" \ 158 | "")" 159 | "")" 160 | echo "${result}" | jq . 161 | ;; 162 | esac 163 | -------------------------------------------------------------------------------- /examples/template.get: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # get template by name 4 | 5 | if [ -e ../zapish.inc ]; then 6 | . ../zapish.inc 7 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 8 | . /usr/libexec/zapish/zapish.inc 9 | else 10 | echo "No zapish.inc found" 11 | exit 1 12 | fi 13 | 14 | zapish_result=$(zabbix_api template.get \ 15 | "$(json_list params \ 16 | "$(json_list "filter" \ 17 | "$(json_array_str "name" \ 18 | "ICMP" \ 19 | "OS Linux" \ 20 | "")" \ 21 | "")" \ 22 | "$(json_str "output" "simple" \ 23 | "")" \ 24 | "")" \ 25 | ) 26 | 27 | echo ${zapish_result} 28 | json_get "${zapish_result}" '.result[].templateid' 29 | -------------------------------------------------------------------------------- /test/empty_zabbix_api_call: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e ../zapish.inc ]; then 4 | . ../zapish.inc 5 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 6 | . /usr/libexec/zapish/zapish.inc 7 | else 8 | echo "No zapish.inc found" 9 | exit 1 10 | fi 11 | 12 | zabbix_api 13 | -------------------------------------------------------------------------------- /test/export_template_by_name: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e ../zapish.inc ]; then 4 | . ../zapish.inc 5 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 6 | . /usr/libexec/zapish/zapish.inc 7 | else 8 | echo "No zapish.inc found" 9 | exit 1 10 | fi 11 | 12 | template_name="ICMP" 13 | 14 | zapish_result=$(zabbix_api template.get \ 15 | $(json_list params \ 16 | $(json_str output simple \ 17 | $(json_list filter \ 18 | $(json_array_str "name" \ 19 | "${template_name}" \ 20 | "") \ 21 | "")) \ 22 | "") 23 | ) 24 | 25 | zapish_result=$(zabbix_api configuration.export \ 26 | $(json_list params \ 27 | $(json_list options \ 28 | $(json_array_num templates \ 29 | $(json_get "${zapish_result}" '.result[].templateid') \ 30 | "") \ 31 | "") \ 32 | $(json_str format xml \ 33 | "") \ 34 | "") 35 | ) 36 | echo ${zapish_result} > ${template_name}.raw 37 | -------------------------------------------------------------------------------- /zapish: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # zapish - Zabbix API SHell command processor 4 | # This program is free software, distributed under the terms of 5 | # the GNU General Public License Version 2. 6 | # 7 | # Copyright (C) 2017-2018 Tomasz Kłoczko 8 | # 9 | 10 | if [ -e ./zapish.inc ]; then 11 | . ./zapish.inc 12 | elif [ -e /usr/libexec/zapish/zapish.inc ]; then 13 | . /usr/libexec/zapish/zapish.inc 14 | else 15 | echo "Not found /usr/libexec/zapish/zapish.inc" 16 | exit 1 17 | fi 18 | 19 | #--------------------------------------------- 20 | # Main commands 21 | 22 | # delete 23 | zapish_acknowledge() { 24 | while [ $# -gt 0 ]; do 25 | cmd="${1}" 26 | shift 27 | case "${cmd}" in 28 | "help" ) 29 | echo " zapish acknowledge " 30 | # TODO: [event] 31 | echo 32 | ;; 33 | esac 34 | done 35 | } 36 | 37 | # delete 38 | zapish_create() { 39 | while [ $# -gt 0 ]; do 40 | cmd="${1}" 41 | shift 42 | case "${cmd}" in 43 | "help" ) 44 | echo " zapish create " 45 | # TODO: [action|screen|slideshow|group|item|host|template|user] 46 | echo 47 | ;; 48 | esac 49 | done 50 | } 51 | 52 | # delete 53 | zapish_delete() { 54 | while [ $# -gt 0 ]; do 55 | cmd="${1}" 56 | shift 57 | case "${cmd}" in 58 | "help" ) 59 | echo " zapish delete " 60 | # TODO: [action|screen|slideshow|group|item|host|template|user] 61 | echo 62 | ;; 63 | esac 64 | done 65 | } 66 | 67 | # export 68 | zapish_export() { 69 | while [ $# -gt 0 ]; do 70 | cmd="${1}" 71 | shift 72 | case "${cmd}" in 73 | "help" ) 74 | echo " zapish export template '' [-o ]" 75 | # TODO: [template] 76 | ;; 77 | "template" ) 78 | obj="${1}" 79 | shift 80 | case "${obj}" in 81 | -d ) 82 | echo "Not implemnted yet directory" 83 | ;; 84 | -f ) 85 | echo "Not implemnted yet format" 86 | ;; 87 | -o ) 88 | echo "Not implemnted yet output" 89 | ;; 90 | * ) 91 | ;; 92 | esac 93 | ;; 94 | esac 95 | 96 | echo 97 | done 98 | } 99 | 100 | # get 101 | zapish_get() { 102 | while [ $# -gt 0 ]; do 103 | cmd="${1}" 104 | shift 105 | case "${cmd}" in 106 | "help" ) 107 | echo " zapish get " 108 | # TODO: [host |template ] 109 | echo 110 | ;; 111 | esac 112 | done 113 | } 114 | 115 | # help 116 | zapish_help() { 117 | echo "Usage:" 118 | zapish_acknowledge help 119 | zapish_create help 120 | zapish_delete help 121 | zapish_export help 122 | zapish_get help 123 | echo " zapish help" 124 | echo 125 | zapish_import help 126 | zapish_update help 127 | } 128 | 129 | # import 130 | zapish_import() { 131 | while [ $# -gt 0 ]; do 132 | cmd="${1}" 133 | shift 134 | case "${cmd}" in 135 | "help" ) 136 | echo " zapish import " 137 | # TODO: [template|regexp] " 138 | echo 139 | ;; 140 | esac 141 | done 142 | } 143 | 144 | # update 145 | zapish_update() { 146 | while [ $# -gt 0 ]; do 147 | cmd="${1}" 148 | shift 149 | case "${cmd}" in 150 | "help" ) 151 | echo " zapish update" 152 | # TODO: [action|host ] 153 | echo 154 | ;; 155 | esac 156 | done 157 | } 158 | 159 | #--------------------------------------------- 160 | # main() 161 | 162 | if [ $# = 0 ]; then 163 | zapish_help 164 | exit 1 165 | fi 166 | 167 | while [ $# -gt 0 ]; do 168 | cmd="${1}" 169 | shift 170 | case "${cmd}" in 171 | "acknowledge" ) 172 | ;; 173 | "create" ) 174 | ;; 175 | "delete" ) 176 | ;; 177 | "export" ) 178 | zapish_export ${*} 179 | ;; 180 | "get" ) 181 | ;; 182 | "help" ) 183 | zapish_help 184 | ;; 185 | "update" ) 186 | ;; 187 | * ) 188 | zapish_help 189 | ;; 190 | esac 191 | shift 192 | done 193 | -------------------------------------------------------------------------------- /zapish.1: -------------------------------------------------------------------------------- 1 | '\" t 2 | .\" Title: zapish 3 | .\" Author: Tomasz Kłoczko 4 | .\" Generator: DocBook XSL Stylesheets vsnapshot 5 | .\" Date: 05/06/2020 6 | .\" Manual: Zabbix API SHell command processor 7 | .\" Source: zapish 8 | .\" Language: English 9 | .\" 10 | .TH "ZAPISH" "1" "05/06/2020" "zapish" "Zabbix API SHell command proce" 11 | .\" ----------------------------------------------------------------- 12 | .\" * Define some portability stuff 13 | .\" ----------------------------------------------------------------- 14 | .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 15 | .\" http://bugs.debian.org/507673 16 | .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html 17 | .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 18 | .ie \n(.g .ds Aq \(aq 19 | .el .ds Aq ' 20 | .\" ----------------------------------------------------------------- 21 | .\" * set default formatting 22 | .\" ----------------------------------------------------------------- 23 | .\" disable hyphenation 24 | .nh 25 | .\" disable justification (adjust text to left margin only) 26 | .ad l 27 | .\" ----------------------------------------------------------------- 28 | .\" * MAIN CONTENT STARTS HERE * 29 | .\" ----------------------------------------------------------------- 30 | .SH "NAME" 31 | zapish \- Zabbix API SHell command processor 32 | .SH "SYNOPSIS" 33 | .HP \w'\ 'u 34 | .PP 35 | zapish 36 | \fBcreate\fR 37 | .PP 38 | zapish 39 | \fBdelete\fR 40 | .PP 41 | zapish 42 | \fBexport\fR 43 | .PP 44 | zapish 45 | \fBget\fR 46 | .PP 47 | zapish 48 | \fBhelp\fR 49 | .PP 50 | zapish 51 | \fBimport\fR 52 | .PP 53 | zapish 54 | \fBupdate\fR 55 | .SH "DESCRIPTION" 56 | .PP 57 | The 58 | \fBzapish\fR 59 | is the SH script which provides easy to use zabbix API CLI interface\&. 60 | .PP 61 | This sctipt uses zapish\&.inc\&. It demostrates how to use zapish\&.inc file in own POSIX SH scripts\&. 62 | .SH "COMMANDS" 63 | .PP 64 | zapish \fBacknowledge\fR 65 | .RS 4 66 | .RE 67 | .PP 68 | zapish \fBcreate\fR 69 | .RS 4 70 | .RE 71 | .PP 72 | zapish \fBdelete\fR 73 | .RS 4 74 | .RE 75 | .PP 76 | zapish \fBexport\fR 77 | .RS 4 78 | .RE 79 | .PP 80 | zapish \fBget\fR 81 | .RS 4 82 | .RE 83 | .PP 84 | zapish \fBhelp\fR 85 | .RS 4 86 | Displays a help message\&. 87 | .RE 88 | .PP 89 | zapish \fBimport\fR 90 | .RS 4 91 | .RE 92 | .PP 93 | zapish \fBupdate\fR 94 | .RS 4 95 | .RE 96 | .SH "CONFIGURATION" 97 | .PP 98 | The 99 | \fBzapish\&.inc\fR 100 | functions uses following configuration variables in 101 | \fB~/\&.zapish\&.rc\fR: 102 | .PP 103 | This scrip use 104 | \fB~/\&.zapish\&.rc\fR 105 | file which is generated on first use zapish\&. 106 | .SH "FILES" 107 | .PP 108 | \fB/usr/libexec/zapish/zapish\&.inc\fR 109 | .RS 4 110 | The default location of the 111 | \fBzapish\&.inc\fR 112 | include file\&. 113 | .RE 114 | .SH "EXIT CODES" 115 | .sp 116 | .it 1 an-trap 117 | .nr an-no-space-flag 1 118 | .nr an-break-flag 1 119 | .br 120 | .B Table\ \&1.\ \& Functions defined in zapish\&.inc may exit with the following exit codes: 121 | .TS 122 | allbox tab(:); 123 | cB lB. 124 | T{ 125 | Exit code 126 | T}:T{ 127 | Description 128 | T} 129 | .T& 130 | c l 131 | c l 132 | c l 133 | c l 134 | c l. 135 | T{ 136 | 0 137 | T}:T{ 138 | Success 139 | T} 140 | T{ 141 | 1 142 | T}:T{ 143 | Zabbix API authentication error 144 | T} 145 | T{ 146 | 2 147 | T}:T{ 148 | First use of zapish\&. Initialization has been done and \fB~/\&.zapish\&.rc\fR file has been created sucessfully 149 | T} 150 | T{ 151 | 3 152 | T}:T{ 153 | The zabbix_api() called without arguments 154 | T} 155 | T{ 156 | 4 157 | T}:T{ 158 | The zabbix_api() call error 159 | T} 160 | .TE 161 | .sp 1 162 | .SH "SEE ALSO" 163 | .PP 164 | \fBzapish.inc\fR(3), 165 | \fBsh\fR(1), 166 | \fBbash\fR(1)\&. 167 | .PP 168 | \m[blue]\fB\%https://github.com/kloczek/zapish/\fR\m[] 169 | .PP 170 | \m[blue]\fB\%https://www.zabbix.com/documentation/3.4/manual/api/\fR\m[] 171 | .SH "AUTHOR" 172 | .PP 173 | \fBTomasz Kłoczko\fR <\&kloczek@fedoraproject\&.org\&> 174 | .RS 4 175 | .RE 176 | .RS 4 177 | Author 178 | .RE 179 | .SH "COPYRIGHT" 180 | .br 181 | Copyright \(co 2018 182 | .br 183 | -------------------------------------------------------------------------------- /zapish.1.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | zapish - Zabbix API SHell command processor 13 | zapish 14 | 15 | 2018 16 | 17 | 18 | 19 | 20 | Tomasz 21 | Kłoczko 22 | 23 |
24 | kloczek@fedoraproject.org 25 |
26 |
27 |
28 | 29 | kloczek 30 | 31 | 32 | Author 33 | 34 |
35 |
36 |
37 | 38 | zapish 39 | 1 40 | Zabbix API SHell command processor 41 | zapish 42 | 43 | 44 | zapish 45 | Zabbix API SHell command processor 46 | 47 | 48 | 49 | zapish create 50 | zapish delete 51 | zapish export 52 | zapish get 53 | zapish help 54 | zapish import 55 | zapish update 56 | 57 | 58 | 59 | DESCRIPTION 60 | 61 | The zapish is the SH script which provides easy to use zabbix API CLI interface. 62 | 63 | 64 | This sctipt uses zapish.inc. It demostrates how to use zapish.inc file in own POSIX SH scripts. 65 | 66 | 67 | 68 | COMMANDS 69 | 70 | 71 | 72 | zapish acknowledge 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | zapish create 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | zapish delete 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | zapish export 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | zapish get 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | zapish help 119 | 120 | Displays a help message. 121 | 122 | 123 | 124 | 125 | 126 | zapish import 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | zapish update 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | CONFIGURATION 146 | 147 | The zapish.inc functions uses following 148 | configuration variables in ~/.zapish.rc: 149 | 150 | 151 | This scrip use ~/.zapish.rc file which is generated on first use zapish. 152 | 153 | 154 | 155 | FILES 156 | 157 | 158 | 159 | /usr/libexec/zapish/zapish.inc 160 | 161 | 162 | The default location of the zapish.inc include file. 163 | 164 | 165 | 166 | 167 | 168 | EXIT CODES 169 | 170 | 171 | Functions defined in <command>zapish.inc</command> may exit with the following exit codes: 172 | 173 | 174 | 175 | 176 | 177 | 178 | Exit code 179 | Description 180 | 181 | 182 | 183 | 184 | 0 185 | Success 186 | 187 | 188 | 1 189 | Zabbix API authentication error 190 | 191 | 192 | 2 193 | 194 | First use of zapish. Initialization has been done and ~/.zapish.rc file has been created sucessfully 195 | 196 | 197 | 198 | 3 199 | The zabbix_api() called without arguments 200 | 201 | 202 | 4 203 | The zabbix_api() call error 204 | 205 | 206 | 207 |
208 |
209 | 210 | SEE ALSO 211 | zapish.inc3, 212 | sh1, 213 | bash1. 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 |
223 | -------------------------------------------------------------------------------- /zapish.inc: -------------------------------------------------------------------------------- 1 | # zapish - Zabbix API SHell binding 2 | # 3 | # This program is free software, distributed under the terms of 4 | # the GNU General Public License Version 2. 5 | # 6 | # Copyright (C) 2017-2018 Tomasz Kłoczko 7 | # 8 | 9 | zapish_url="" 10 | zapish_auth="" 11 | 12 | zapish_request="" 13 | 14 | json_str() { 15 | echo -n "\"$1\":\"$2\"" 16 | shift 2 17 | while [ -n "$1" ]; do 18 | echo -n ",$1" 19 | shift 1 20 | done 21 | } 22 | 23 | json_num() { 24 | echo -n "\"$1\":$2" 25 | shift 2 26 | while [ -n "$1" ]; do 27 | echo -n ",$1" 28 | shift 1 29 | done 30 | } 31 | 32 | json_list() { 33 | echo -n "\"$1\":{$2" 34 | shift 2 35 | while [ -n "$1" ]; do 36 | echo -n ",$1" 37 | shift 1 38 | done 39 | echo -n "}" 40 | } 41 | 42 | json_array_num() { 43 | echo -n "\"$1\":[$2" 44 | shift 2 45 | while [ -n "$1" ]; do 46 | echo -n ",$1" 47 | shift 1 48 | done 49 | echo -n "]" 50 | } 51 | 52 | json_array_str() { 53 | echo -n "\"$1\":[\"$2\"" 54 | shift 2 55 | while [ -n "$1" ]; do 56 | echo -n ",\"$1\"" 57 | shift 1 58 | done 59 | echo -n "]" 60 | } 61 | 62 | #==================================== 63 | # Zabbix result processor 64 | #==================================== 65 | 66 | json_get() { 67 | # for i in $(eval "echo {1..${#1}}"); do 68 | # echo -n "${1:$((i-1)):1} " 69 | # done 70 | echo "$1" | jq "$2" 71 | } 72 | 73 | json_error() { 74 | if [ "$(json_get "${1}" .error.code)" != "null" ]; then 75 | json_get "${1}" .error 76 | exit 4 77 | else 78 | echo "$1" 79 | fi 80 | } 81 | 82 | #==================================== 83 | # Zabbix API caller 84 | #==================================== 85 | 86 | zabbix_api() { 87 | if [ -z "$1" ]; then 88 | echo "zabbix_api() function expects a list of parameters describing API call" 89 | exit 3 90 | fi 91 | 92 | zapish_request="{$(json_str jsonrpc "2.0" \ 93 | "$(json_str method "$1" \ 94 | "$2" \ 95 | "$(json_str auth $zapish_auth "")" \ 96 | "$(json_num id 0 "")" \ 97 | "")" \ 98 | )}" 99 | 100 | json_error "$(curl --silent -X POST -H \ 101 | 'Content-Type: application/json' -d \ 102 | "$zapish_request" $zapish_url)" 103 | } 104 | 105 | #==================================== 106 | # Check and initialize authentication 107 | #==================================== 108 | 109 | zapish_init() { 110 | echo -n "Enter URL of the Zabbix API gateway [http://localhost/api_jsonrpc.php]: " 111 | read zapish_url 112 | if [ ! -n "$zapish_url" ]; then 113 | zapish_url="http://localhost/api_jsonrpc.php" 114 | fi 115 | 116 | echo -n "Enter Zabbix API account name [Admin]: " 117 | read zapish_user 118 | if [ ! -n "$zapish_user" ]; then 119 | zapish_user="Admin" 120 | fi 121 | 122 | echo -n "Enter Zabbix API account password [zabbix]: " 123 | read zapish_pwd 124 | if [ ! -n "$zapish_pwd" ]; then 125 | zapish_pwd="zabbix" 126 | fi 127 | 128 | zapish_request="{""$(json_str jsonrpc "2.0" \ 129 | $(json_str method user.login \ 130 | $(json_list params \ 131 | $(json_str user "$zapish_user" \ 132 | "") \ 133 | $(json_str password "$zapish_pwd" \ 134 | "") \ 135 | "") \ 136 | $(json_num id 0 \ 137 | "") \ 138 | ))""}" 139 | 140 | zapish_result="$(curl --silent -X POST -H \ 141 | 'Content-Type: application/json' -d "${zapish_request}" ${zapish_url})" 142 | 143 | if [ "$(json_get "${zapish_result}" .error )" != "null" ]; then 144 | echo "Zabbix API authentication error." 145 | exit 1 146 | fi 147 | 148 | echo zapish_url=\"$zapish_url\" > ~/.zapish.rc 149 | echo zapish_auth=$(json_get "${zapish_result}" .result) >> ~/.zapish.rc 150 | chmod 600 ~/.zapish.rc 151 | echo "Zabbix API authentication successful. No Zabbix API call." 152 | echo "zapish authentication token has been stored in ~/.zapish.rc" 153 | exit 2 154 | } 155 | 156 | # initialization 157 | if [ -f ~/.zapish.rc ]; then 158 | . ~/.zapish.rc 159 | else 160 | zapish_init 161 | fi 162 | -------------------------------------------------------------------------------- /zapish.inc.3: -------------------------------------------------------------------------------- 1 | '\" t 2 | .\" Title: zapish.inc 3 | .\" Author: Tomasz Kłoczko 4 | .\" Generator: DocBook XSL Stylesheets vsnapshot 5 | .\" Date: 06/11/2018 6 | .\" Manual: Library functions 7 | .\" Source: zapish.inc 8 | .\" Language: English 9 | .\" 10 | .TH "ZAPISH\&.INC" "3" "06/11/2018" "zapish\&.inc" "Library functions" 11 | .\" ----------------------------------------------------------------- 12 | .\" * Define some portability stuff 13 | .\" ----------------------------------------------------------------- 14 | .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 15 | .\" http://bugs.debian.org/507673 16 | .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html 17 | .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 18 | .ie \n(.g .ds Aq \(aq 19 | .el .ds Aq ' 20 | .\" ----------------------------------------------------------------- 21 | .\" * set default formatting 22 | .\" ----------------------------------------------------------------- 23 | .\" disable hyphenation 24 | .nh 25 | .\" disable justification (adjust text to left margin only) 26 | .ad l 27 | .\" ----------------------------------------------------------------- 28 | .\" * MAIN CONTENT STARTS HERE * 29 | .\" ----------------------------------------------------------------- 30 | .SH "NAME" 31 | zapish.inc \- Zabbix API SHell binding 32 | .SH "SYNOPSIS" 33 | .HP \w'\fB\&.\ /usr/libexec/zapish\&.inc\fR\ 'u 34 | \fB\&. /usr/libexec/zapish\&.inc\fR 35 | .SH "DESCRIPTION" 36 | .PP 37 | The 38 | \fBzapish\&.inc\fR 39 | is the file to include in own SH/bash scripts with few functions which make calling Zabbix API easy\&. 40 | .PP 41 | It consists of few functions for: 42 | .sp 43 | .RS 4 44 | .ie n \{\ 45 | \h'-04'\(bu\h'+03'\c 46 | .\} 47 | .el \{\ 48 | .sp -1 49 | .IP \(bu 2.3 50 | .\} 51 | construct JSON string fields like string, numeric and arrays 52 | .RE 53 | .sp 54 | .RS 4 55 | .ie n \{\ 56 | \h'-04'\(bu\h'+03'\c 57 | .\} 58 | .el \{\ 59 | .sp -1 60 | .IP \(bu 2.3 61 | .\} 62 | the main 63 | \fBzabbix_api\fR 64 | function which is used to call Zabbix API 65 | .RE 66 | .sp 67 | .RS 4 68 | .ie n \{\ 69 | \h'-04'\(bu\h'+03'\c 70 | .\} 71 | .el \{\ 72 | .sp -1 73 | .IP \(bu 2.3 74 | .\} 75 | JSON return values parser/extractor 76 | .RE 77 | .sp 78 | .RS 4 79 | .ie n \{\ 80 | \h'-04'\(bu\h'+03'\c 81 | .\} 82 | .el \{\ 83 | .sp -1 84 | .IP \(bu 2.3 85 | .\} 86 | the 87 | \fBzapish_init\fR 88 | function which reads 89 | \fB~/\&.zapish\&.rc\fR 90 | configuration file or initialize if it does not exist 91 | .RE 92 | .PP 93 | At the moment zapish\&.inc is not fully POSIX SH compliant because it uses in few places "local" keyword to define functions local variables\&. However, the intention is to rewrite it to make it fully POSIX SH compliant code\&. 94 | .SH "FUNCTIONS" 95 | .PP 96 | \fBNOTE:\fRBelow all json_* functions as last argument must have empty string ("")\&. 97 | .PP 98 | \fBjson_array_num\fR 99 | .RS 4 100 | This function prints string in the format of the JSON table of numbers\&. 101 | .sp 102 | Syntax: 103 | \fBjson_array_num\fR 104 | "array_name" num1 [[num2] \&.\&.\&.] "" 105 | .RE 106 | .PP 107 | \fBjson_array_str\fR 108 | .RS 4 109 | This function prints string in the format of the JSON table of strings\&. 110 | .sp 111 | Syntax: 112 | \fBjson_array_str\fR 113 | "array_name" "str1" [["str2"] \&.\&.\&.] "" 114 | .RE 115 | .PP 116 | \fBjson_list\fR 117 | .RS 4 118 | This function prints string in the format of the JSON list of unspecified elements\&. 119 | .RE 120 | .PP 121 | \fBjson_num\fR 122 | .RS 4 123 | This function prints list of parameters in the format of the JSON field with list of numbers\&. 124 | .sp 125 | Syntax: 126 | \fBjson_num\fR 127 | "list_num_name" num1 [[num2] \&.\&.\&.] "" 128 | .RE 129 | .PP 130 | \fBjson_str\fR 131 | .RS 4 132 | This function prints list of parameters in the format of the JSON field with list of strings\&. 133 | .sp 134 | Syntax: 135 | \fBjson_num\fR 136 | "list_str_name" "str1" [["str2"] \&.\&.\&.] "" 137 | .RE 138 | .PP 139 | \fBzabbix_api\fR 140 | .RS 4 141 | The main zapish function used to call any Zabbix API\&. This function expects only one parameter in which should be 142 | .RE 143 | .SH "CONFIGURATION" 144 | .PP 145 | The 146 | \fBzapish\&.inc\fR 147 | functions uses following configuration variables in 148 | \fB~/\&.zapish\&.rc\fR: 149 | .PP 150 | \fBzapish_url\fR 151 | .RS 4 152 | URL of the Zabbix API gateway used by 153 | \fBzapish\&.inc\fR\&. 154 | .RE 155 | .PP 156 | \fBzapish_auth\fR 157 | .RS 4 158 | Zabbix API authentication token generated by 159 | \fBuser\&.login\fR 160 | API function\&. 161 | .RE 162 | .PP 163 | The 164 | \fB~/\&.zapish\&.rc\fR 165 | file is generated automatically if it does not exist by any script which includes 166 | \fBzapish\&.inc\fR\&. It will prompt to enter the username, password and the Zabbix API gateway URL\&. If authentication will be correct generated authentication token hash and API gateway URL will be stored in 167 | \fB~/\&.zapish\&.rc\fR\&. 168 | .SH "FILES" 169 | .PP 170 | \fB/usr/libexec/zapish/zapish\&.inc\fR 171 | .RS 4 172 | The default location of the 173 | \fBzapish\&.inc\fR 174 | include file\&. 175 | .RE 176 | .SH "EXIT CODES" 177 | .sp 178 | .it 1 an-trap 179 | .nr an-no-space-flag 1 180 | .nr an-break-flag 1 181 | .br 182 | .B Table\ \&1.\ \& Functions defined in zapish\&.inc may exit with the following exit codes: 183 | .TS 184 | allbox tab(:); 185 | cB lB. 186 | T{ 187 | Exit code 188 | T}:T{ 189 | Description 190 | T} 191 | .T& 192 | c l 193 | c l 194 | c l 195 | c l 196 | c l. 197 | T{ 198 | 0 199 | T}:T{ 200 | Success 201 | T} 202 | T{ 203 | 1 204 | T}:T{ 205 | Zabbix API authentication error 206 | T} 207 | T{ 208 | 2 209 | T}:T{ 210 | First use of zapish\&. Initialization has been done and \fB~/\&.zapish\&.rc\fR file has been created sucessfully 211 | T} 212 | T{ 213 | 3 214 | T}:T{ 215 | The zabbix_api() called without arguments 216 | T} 217 | T{ 218 | 4 219 | T}:T{ 220 | The zabbix_api() call error 221 | T} 222 | .TE 223 | .sp 1 224 | .SH "SEE ALSO" 225 | .PP 226 | \fBsh\fR(1), 227 | \fBbash\fR(1)\&. 228 | .PP 229 | \m[blue]\fB\%https://github.com/kloczek/zapish/\fR\m[] 230 | .PP 231 | \m[blue]\fB\%https://www.zabbix.com/documentation/3.4/manual/api/\fR\m[] 232 | .SH "AUTHOR" 233 | .PP 234 | \fBTomasz Kłoczko\fR <\&kloczek@fedoraproject\&.org\&> 235 | .RS 4 236 | .RE 237 | .RS 4 238 | Author 239 | .RE 240 | .SH "COPYRIGHT" 241 | .br 242 | Copyright \(co 2017-2018 243 | .br 244 | -------------------------------------------------------------------------------- /zapish.inc.3.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | zapish.inc Manual 13 | zapish 14 | 15 | 2017-2018 16 | 17 | 18 | 19 | 20 | Tomasz 21 | Kłoczko 22 | 23 |
24 | kloczek@fedoraproject.org 25 |
26 |
27 |
28 | 29 | kloczek 30 | 31 | 32 | Author 33 | 34 |
35 |
36 |
37 | 38 | zapish.inc 39 | 3 40 | Library functions 41 | zapish.inc 42 | 43 | 44 | zapish.inc 45 | Zabbix API SHell binding 46 | 47 | 48 | 49 | . /usr/libexec/zapish.inc 50 | 51 | 52 | 53 | DESCRIPTION 54 | 55 | The zapish.inc is the file to include in own 56 | SH/bash scripts with few functions which make calling Zabbix API easy. 57 | 58 | 59 | It consists of few functions for: 60 | 61 | 62 | 63 | 64 | construct JSON string fields like string, numeric and arrays 65 | 66 | 67 | 68 | 69 | the main zabbix_api function which is used 70 | to call Zabbix API 71 | 72 | 73 | 74 | 75 | JSON return values parser/extractor 76 | 77 | 78 | 79 | 80 | the zapish_init function which reads 81 | ~/.zapish.rc configuration file or initialize 82 | if it does not exist 83 | 84 | 85 | 86 | 87 | At the moment zapish.inc is not fully POSIX SH compliant because it 88 | uses in few places "local" keyword to define functions local 89 | variables. However, the intention is to rewrite it to make it fully 90 | POSIX SH compliant code. 91 | 92 | 93 | 94 | FUNCTIONS 95 | NOTE:Below all json_* functions as last argument must have empty string (""). 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | This function prints string in the format of the JSON table of numbers. 104 | 105 | 106 | Syntax: json_array_num "array_name" num1 [[num2] ...] "" 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | This function prints string in the format of the JSON table of strings. 117 | 118 | 119 | Syntax: json_array_str "array_name" "str1" [["str2"] ...] "" 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | This function prints string in the format of the JSON list of unspecified elements. 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | This function prints list of parameters in the format of the JSON field with list of numbers. 140 | 141 | 142 | Syntax: json_num "list_num_name" num1 [[num2] ...] "" 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | This function prints list of parameters in the format of the JSON field with list of strings. 153 | 154 | 155 | Syntax: json_num "list_str_name" "str1" [["str2"] ...] "" 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | The main zapish function used to call any Zabbix API. This function 166 | expects only one parameter in which should be 167 | 168 | 169 | 170 | 171 | 172 | 173 | CONFIGURATION 174 | 175 | The zapish.inc functions uses following 176 | configuration variables in ~/.zapish.rc: 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | URL of the Zabbix API gateway used by 185 | zapish.inc. 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | Zabbix API authentication token generated by 194 | user.login API function. 195 | 196 | 197 | 198 | 199 | The ~/.zapish.rc file is generated automatically if 200 | it does not exist by any script which includes 201 | zapish.inc. It will prompt to enter the username, 202 | password and the Zabbix API gateway URL. If authentication will be 203 | correct generated authentication token hash and API gateway URL will 204 | be stored in ~/.zapish.rc. 205 | 206 | 207 | 208 | FILES 209 | 210 | 211 | 212 | /usr/libexec/zapish/zapish.inc 213 | 214 | 215 | The default location of the zapish.inc include file. 216 | 217 | 218 | 219 | 220 | 221 | EXIT CODES 222 | 223 | 224 | Functions defined in <command>zapish.inc</command> may exit with the following exit codes: 225 | 226 | 227 | 228 | 229 | 230 | 231 | Exit code 232 | Description 233 | 234 | 235 | 236 | 237 | 0 238 | Success 239 | 240 | 241 | 1 242 | Zabbix API authentication error 243 | 244 | 245 | 2 246 | 247 | First use of zapish. Initialization has been done and ~/.zapish.rc file has been created sucessfully 248 | 249 | 250 | 251 | 3 252 | The zabbix_api() called without arguments 253 | 254 | 255 | 4 256 | The zabbix_api() call error 257 | 258 | 259 | 260 |
261 |
262 | 263 | SEE ALSO 264 | sh1, 265 | bash1. 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 |
275 | --------------------------------------------------------------------------------