mirror of
https://github.com/limosek/zaf.git
synced 2025-11-01 10:07:38 +01:00
Working on 1.1 version
Added cache support Added Zabbix API calls
This commit is contained in:
59
lib/cache.lib.sh
Normal file
59
lib/cache.lib.sh
Normal file
@@ -0,0 +1,59 @@
|
||||
# Zaf cache related functions
|
||||
|
||||
zaf_cache_init(){
|
||||
[ -n "$ZAF_CACHE_DIR" ] && rm -rf "$ZAF_CACHE_DIR"
|
||||
mkdir -p "$ZAF_CACHE_DIR"
|
||||
}
|
||||
|
||||
# Get cache key from requested param
|
||||
zaf_cachekey(){
|
||||
echo "$1" | md5sum - | cut -d ' ' -f 1
|
||||
}
|
||||
|
||||
# Put object into cache
|
||||
# $1 key
|
||||
# $2 value
|
||||
# $3 lifetime in seconds
|
||||
zaf_tocache(){
|
||||
local key
|
||||
local value
|
||||
local lifetime
|
||||
key=$(zaf_cachekey "$1")
|
||||
echo "$2" >$ZAF_CACHE_DIR/$key
|
||||
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.tme
|
||||
}
|
||||
|
||||
# Put object into cache from stdin and copy to stdout
|
||||
# $1 key
|
||||
# $2 lifetime in seconds
|
||||
zaf_tocache_stdin(){
|
||||
local key
|
||||
local lifetime
|
||||
|
||||
key=$(zaf_cachekey "$1")
|
||||
cat >$ZAF_CACHE_DIR/$key
|
||||
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.tme
|
||||
cat $ZAF_CACHE_DIR/$key
|
||||
}
|
||||
|
||||
# Get object from cache
|
||||
# $1 key
|
||||
zaf_fromcache(){
|
||||
local key
|
||||
local value
|
||||
key=$(zaf_cachekey "$1")
|
||||
if [ -f $ZAF_CACHE_DIR/$key ]; then
|
||||
if [ "$ZAF_CACHE_DIR/$key.tme" -nt "$ZAF_CACHE_DIR/$key" ]; then
|
||||
zaf_trc "Cache: serving $1($key) from cache"
|
||||
cat $ZAF_CACHE_DIR/$key
|
||||
else
|
||||
zaf_trc "Cache: removing old entry $1"
|
||||
rm -f "$ZAF_CACHE_DIR/$key*"
|
||||
return 2
|
||||
fi
|
||||
else
|
||||
zaf_trc "Cache: missing entry $1($key)"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
# Hardcoded variables
|
||||
ZAF_VERSION="master"
|
||||
ZAF_VERSION="1.1"
|
||||
ZAF_GITBRANCH="master"
|
||||
ZAF_URL="https://github.com/limosek/zaf"
|
||||
ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf"
|
||||
|
||||
@@ -9,6 +10,9 @@ ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf"
|
||||
zaf_msg() {
|
||||
echo $@
|
||||
}
|
||||
zaf_trc() {
|
||||
[ "$ZAF_DEBUG" -ge "3" ] && logger -s -t zaf -- $@
|
||||
}
|
||||
zaf_dbg() {
|
||||
[ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf -- $@
|
||||
}
|
||||
@@ -32,7 +36,11 @@ zaf_fetch_url() {
|
||||
local scheme
|
||||
local uri
|
||||
local insecure
|
||||
local out
|
||||
|
||||
if zaf_fromcache "$1"; then
|
||||
return
|
||||
fi
|
||||
scheme=$(echo $1|cut -d ':' -f 1)
|
||||
uri=$(echo $1|cut -d '/' -f 3-)
|
||||
if [ "$1" = "$scheme" ]; then
|
||||
@@ -43,7 +51,7 @@ zaf_fetch_url() {
|
||||
[ "$scheme" != "file" ] && [ -n "$ZAF_OFFLINE" ] && zaf_err "Cannot download $1 in offline mode!"
|
||||
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
|
||||
zaf_dbg curl $insecure -f -s -L -o - $1
|
||||
curl $insecure -f -s -L -o - "$1"
|
||||
curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120
|
||||
;;
|
||||
esac
|
||||
}
|
||||
@@ -238,8 +246,9 @@ zaf_install_plugin() {
|
||||
plugin=$(zaf_ctrl_get_global_block <"${ZAF_TMP_DIR}/plugin/control.zaf" | zaf_block_get_option Plugin)
|
||||
plugindir="${ZAF_PLUGINS_DIR}"/$plugin
|
||||
if [ -n "$plugin" ] && zaf_prepare_plugin "$1" $plugindir; then
|
||||
zaf_wrn "Installing plugin $plugin from $url to $plugindir"
|
||||
control=${plugindir}/control.zaf
|
||||
[ "$ZAF_DEBUG" -gt 0 ] && zaf_plugin_info "${control}"
|
||||
[ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}"
|
||||
zaf_ctrl_check_deps "${control}"
|
||||
zaf_ctrl_install "$url" "${control}" "${plugindir}"
|
||||
zaf_ctrl_generate_cfg "${control}" "${plugin}" \
|
||||
|
||||
151
lib/zbxapi.lib.sh
Executable file → Normal file
151
lib/zbxapi.lib.sh
Executable file → Normal file
@@ -1,4 +1,35 @@
|
||||
#!/bin/sh
|
||||
# Call api function
|
||||
# $1 - query string
|
||||
zaf_zbxapi_do() {
|
||||
local result
|
||||
if ! zaf_fromcache "$1"; then
|
||||
zaf_dbg "Zabbix API: $1"
|
||||
result=$(curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL")
|
||||
zaf_tocache "$1" "$result" 60
|
||||
echo $result
|
||||
fi
|
||||
}
|
||||
|
||||
# Extract result from JSON response
|
||||
zaf_zbxapi_getresult() {
|
||||
sed -e 's/\({"jsonrpc":"2.0","result":\)\(.*\),\("id":.*\)/\2/g' | sed -e 's/^\[\]$//'
|
||||
}
|
||||
|
||||
# Extract XML result from JSON response
|
||||
zaf_zbxapi_getxml() {
|
||||
zaf_zbxapi_getresult | sed -e 's/{"jsonrpc":"2.0","result":"//' | sed -e 's/","id"\:1}//' | sed -e 's#\\\([<">/]\)#\1#g'
|
||||
}
|
||||
|
||||
# Extract string from JSON response result
|
||||
zaf_zbxapi_getstring() {
|
||||
sed -e 's/^"//' -e 's/"$//' -e 's/\\n/'\\n'/g'
|
||||
}
|
||||
|
||||
# Extract value from JSON response result
|
||||
# $1 key
|
||||
zaf_zbxapi_getvalue() {
|
||||
tr ',' '\n' | grep "\"$1\":" | cut -d '"' -f 4
|
||||
}
|
||||
|
||||
# Zabbix API related functions
|
||||
# Parameters in global variables ZAF_ZBX_API_*
|
||||
@@ -7,43 +38,124 @@ zaf_zbxapi_login(){
|
||||
local authstr
|
||||
local user
|
||||
local pass
|
||||
local result
|
||||
|
||||
[ -z "$ZAF_ZBXAPI_URL" ] || [ -z "$ZAF_ZBXAPI_USER" ] || [ -z "$ZAF_ZBXAPI_PASS" ] && zaf_err "Zabbix Api parameters not set!"
|
||||
authstr='{
|
||||
"method" : "user.login",
|
||||
"params" : {
|
||||
"password" : "'$ZAF_ZBXAPI_PASS'",
|
||||
"user" : "'$ZAF_ZBXAPI_USER'"
|
||||
},
|
||||
"id" : 0,
|
||||
"jsonrpc" : "2.0",
|
||||
"method" : "user.login"
|
||||
"jsonrpc" : "2.0"
|
||||
}'
|
||||
zaf_dbg "Zabbix API login: $authstr"
|
||||
ZAF_ZBXAPI_AUTH=$(curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$authstr" "$ZAF_ZBXAPI_URL" | json_pp | grep result | cut -d ':' -f 2 | tr -d '", ') || { zaf_err "Bad zabbix API parameters, cannot login."; }
|
||||
|
||||
if [ "$ZAF_ZBXAPI_AUTHTYPE" = "http" ] ; then
|
||||
ZAF_ZBXAPI_URL=$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 1)//$ZAF_ZBXAPI_USER:$ZAF_ZBXAPI_PASS@$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 3-)
|
||||
fi
|
||||
result=$(zaf_zbxapi_do "$authstr")
|
||||
echo $result | grep -vq '"result":"' && zaf_err "Cannot login to API"
|
||||
ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getresult| zaf_zbxapi_getstring)
|
||||
zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)"
|
||||
}
|
||||
|
||||
zaf_zbxapi_getxml() {
|
||||
sed -e 's/\({"jsonrpc":"2.0","result":\)"\(.*\)",\("id":.*\)/\n\2\n/g' | sed -r 's/\\([/"])/\1/g'
|
||||
# $1 hostgroup name
|
||||
zaf_zbxapi_gethostgroupid() {
|
||||
local hstr
|
||||
local filter
|
||||
local gfilter
|
||||
|
||||
hstr='{
|
||||
"method": "hostgroup.get",
|
||||
"jsonrpc": "2.0",
|
||||
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
||||
"params": {
|
||||
"filter": {
|
||||
"name": ["'$1'"]
|
||||
},
|
||||
"output": "shorten"
|
||||
},
|
||||
"id": 1
|
||||
}'
|
||||
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
|
||||
}
|
||||
|
||||
# $1 host group or empty
|
||||
zaf_zbxapi_gethosts() {
|
||||
# $1 hostname
|
||||
zaf_zbxapi_gethostid() {
|
||||
local hstr
|
||||
local hgroup
|
||||
local host
|
||||
local groupid
|
||||
local filter
|
||||
local gfilter
|
||||
|
||||
hgroup="$1"
|
||||
[ -n "$hgroup" ] && filter='"filter": { "hostgroup": [ "'$hgroup'" ] },'
|
||||
host="$1"
|
||||
if [ -n "$host" ] ; then
|
||||
filter='"filter": { "host": [ "'$host'" ] },'
|
||||
fi
|
||||
hstr='{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "host.get",
|
||||
"jsonrpc": "2.0",
|
||||
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
||||
'$filter'
|
||||
"id": 2
|
||||
"params": {
|
||||
'$filter'
|
||||
"output": "shorten"
|
||||
},
|
||||
"id": 1
|
||||
}'
|
||||
zaf_dbg "Zabbix Get hosts: $hstr"
|
||||
curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$hstr" "$ZAF_ZBXAPI_URL" | tr ',' '\n' | grep hostid | cut -d '"' -f 4
|
||||
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
|
||||
}
|
||||
|
||||
# $1 hostid
|
||||
zaf_zbxapi_gethost() {
|
||||
local hstr
|
||||
local host
|
||||
local groupid
|
||||
local filter
|
||||
local gfilter
|
||||
|
||||
hostid="$1"
|
||||
if [ -n "$hostid" ] ; then
|
||||
filter='"hostids": [ "'$hostid'" ],'
|
||||
fi
|
||||
hstr='{
|
||||
"method": "host.get",
|
||||
"jsonrpc": "2.0",
|
||||
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
||||
"params": {
|
||||
'$filter'
|
||||
"output": "extend"
|
||||
},
|
||||
"id": 1
|
||||
}'
|
||||
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | zaf_zbxapi_getvalue host
|
||||
}
|
||||
|
||||
# $1 hostgroupid
|
||||
zaf_zbxapi_gethostsingroup() {
|
||||
local hstr
|
||||
local host
|
||||
local groupid
|
||||
local filter
|
||||
local gfilter
|
||||
|
||||
groupid="$1"
|
||||
if [ -n "$groupid" ]; then
|
||||
gfilter='"groupids": [ "'$groupid'" ],'
|
||||
fi
|
||||
|
||||
hstr='{
|
||||
"method": "host.get",
|
||||
"jsonrpc": "2.0",
|
||||
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
||||
"params": {
|
||||
'$gfilter'
|
||||
'$filter'
|
||||
"output": "shorten"
|
||||
},
|
||||
"id": 1
|
||||
}'
|
||||
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
|
||||
}
|
||||
|
||||
# Host backup
|
||||
@@ -54,8 +166,8 @@ zaf_zbxapi_backuphost(){
|
||||
host="$1"
|
||||
bkpstr='
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "configuration.export",
|
||||
"jsonrpc": "2.0",
|
||||
"params": {
|
||||
"options": {
|
||||
"hosts": [
|
||||
@@ -67,8 +179,7 @@ zaf_zbxapi_backuphost(){
|
||||
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
||||
"id": 1
|
||||
}'
|
||||
zaf_dbg "Zabbix API backup host: $bkpstr"
|
||||
curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$bkpstr" "$ZAF_ZBXAPI_URL" | zaf_zbxapi_getxml
|
||||
zaf_zbxapi_do "$bkpstr" | zaf_zbxapi_getxml
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user