INT 21h

Hi, I am Vladimir Smagin, SysAdmin and Kaptain. Telegram Email / GIT / Thingiverse / RSS / GPG

Zabbix: notifications to Telegram

№ 9548 В разделе "Sysadmin" от November 15th, 2018,
В подшивках: , ,

To receive and forward messages to Telegram you need to install my Telegram-post-bot or use docker image. After this you can create script /usr/lib/zabbix/alertscripts/telegram.sh with this content:

#!/bin/bash
source /etc/profile

json_escape () {
    printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()).strip("\""))'
}

chat_id="-763495760089" # your channel id
post_title=$(json_escape "$1")
post_body=$(json_escape "$2")

echo "{\"chatID\":\"${chat_id}\",\"message\":\"${post_title}\n\n${post_body}\"}" | nc -U /run/telegram-GrapeBlessedBot.sock

You can get your account, group or channel id easily using @get_id_bot.

Now you ready to add new Media type (Administration -> Media types)

Add new media to your account

That’s all folks!

Нет комментариев »

Zabbix: monitor bash script results

№ 9533 В разделе "Sysadmin" от November 10th, 2018,
В подшивках: , ,

Install zabbix-agent (sometimes zabbix-sender) package to your server that contains zabbix_sender. Now add some exit codes checks after every monitored command.

#!/bin/bash
set -e
source /etc/profile

ftp_user="username"
ftp_pass="pas5w0rd"
ftp_serv="myftp.server.ru"

pg_user="dbusername"
pg_pass="dbpasswd"
pg_host="127.0.0.1"
pg_port="5432"
pg_dbname="mydatabase"

weekday=$(date '+%w') # 7 days history
history=$weekday

docker exec -it pg2-server su -c "pg_dump postgresql://${pg_user}:${pg_pass}@${pg_host}:${pg_port}/ \
    ${pg_dbname}" postgres |gzip -7 > /tmp/db_backup_${pg_dbname}_${history}.sql.gz
if [ $? -ne 0 ]
then
    echo "pg_dump failed to dump"
    /usr/bin/zabbix_sender -z zabbix.server.ru -s pg2-server -k pgdumper.dump -o failed
    rm -f /tmp/db_backup_${pg_dbname}_${history}.sql.gz
    exit 1
else
    /usr/bin/zabbix_sender -z zabbix.server.ru -s pg2-server -k pgdumper.dump -o successful
    curl --upload-file /tmp/db_backup_${pg_dbname}_${history}.sql.gz ftp://${ftp_user}:${ftp_pass}@${ftp_serv}/
    if [ $? -ne 0 ]
        then
            echo "curl failed to upload dumped file"
            /usr/bin/zabbix_sender -z zabbix.server.ru -s pg2-server -k pgdumper.upload -o failed
            rm -f /tmp/db_backup_${pg_dbname}_${history}.sql.gz
            exit 1
        fi
    /usr/bin/zabbix_sender -z zabbix.server.ru -s pg2-server -k pgdumper.upload -o successful
    rm -f /tmp/db_backup_${pg_dbname}_${history}.sql.gz
fi

Create template

Add your monitored items

And add triggers to raise exceptions

You can combine items in one trigger

Do not forget to activate email notices.

Нет комментариев »

Zabbix: удаление хоста из консоли

№ 9051 В разделе "Sysadmin" от July 7th, 2018,
В подшивках: , ,

Требует установки пакета jq

Скрипт для вызова из консоли с передачей параметров

#!/bin/bash

HOST_NAME="$3"

USER="$1"
PASS="$2"
ZABBIX_SERVER='monitor.server.com'
API="http://$ZABBIX_SERVER/zabbix/api_jsonrpc.php"

authenticate() {
    echo `curl -s -H  'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"user.login\",\"params\":{\"user\":\""${USER}"\",\"password\":\""${PASS}"\"},\"auth\": null,\"id\":0}" $API`
}

AUTH_TOKEN=`echo $(authenticate)|jq -r .result`

gethostid() {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.get\",\"params\":{\"output\":\"extend\",\"filter\":{\"host\":[\""$HOST_NAME"\"]}},\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`
}

HOST_ID=`echo $(gethostid)|jq -r .result[0].hostid`

remove_host() {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.delete\",\"params\":[\""${HOST_ID}"\"],\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`
}
RESPONSE=$(remove_host)
echo ${RESPONSE}

Скрипт для запуска на виртуальной машине перед тем, как она будет выключена. Пригодна для использования на autoscale фермах Amazon.

#!/bin/bash
# Remove-Zabbix Init script should run when an AWS instance goes down and remove itself from Zabbix Server
# chkconfig: - 84 02
# description: Remove from zabbix
# Source function library.
. /etc/init.d/functions

start() {
/bin/touch /var/lock/subsys/Remove-Zabbix
}

stop() {
        /etc/init.d/zabbix-agent stop
        /bin/rm -f /var/lock/subsys/Remove-Zabbix
        HOST_NAME=`echo $(hostname)`

        USER='username' 
        PASS='password' 
        ZABBIX_SERVER='monitor.server.com'
        API="http://$ZABBIX_SERVER/zabbix/api_jsonrpc.php"

        # Authenticate with Zabbix API

        authenticate() {
                echo `curl -s -H  'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"user.login\",\"params\":{\"user\":\""${USER}"\",\"password\":\""${PASS}"\"},\"auth\": null,\"id\":0}" $API`
        }

        AUTH_TOKEN=`echo $(authenticate)|jq -r .result`

        # Get This Host HostId:

        gethostid() {
               echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.get\",\"params\":{\"output\":\"extend\",\"filter\":{\"host\":[\""$HOST_NAME"\"]}},\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`
        }

        HOST_ID=`echo $(gethostid)|jq -r .result[0].hostid`

        # Remove Host

        remove_host() {
                echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.delete\",\"params\":[\""${HOST_ID}"\"],\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`
        }
        RESPONSE=$(remove_host)
        echo ${RESPONSE}
}

case $1 in

        start)
          start
        ;;

        stop)  
          stop
        ;;

        restart)
          stop
          start
        ;;

esac    
exit 0

Нет комментариев »

Zabbix: nginx stub_status monitoring

№ 9029 В разделе "Sysadmin" от June 15th, 2018,
В подшивках: , , ,

stub_status in nginx looks like this

root@boroda:~# curl -s blindage.org/status 
Active connections: 2 
server accepts handled requests
 231167 231167 496908 
Reading: 0 Writing: 1 Waiting: 1 

Counters helps you to understand some statistics of your server and you can monitor this metrics in Zabbix.

Open Nginx config file and add to server section with host’s name from zabbix:

    location /status {
        allow 127.0.0.1; #allow only local agent
        deny all;
        stub_status on;
        access_log off;
    }

Create file /etc/zabbix-agent/zabbix_agentd.d/nginx.conf:

UserParameter=nginx.active[*],wget -O- -q $1/status | awk '/^Active/ {print $NF}'
UserParameter=nginx.reading[*],wget -O- -q $1/status | awk '/Reading/ {print $$2}'
UserParameter=nginx.writing[*],wget -O- -q $1/status | awk '/Writing/ {print $$4}'
UserParameter=nginx.waiting[*],wget -O- -q $1/status | awk '/Waiting/ {print $$6}'
UserParameter=nginx.accepted[*],wget -O- -q $1/status | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$1}'
UserParameter=nginx.handled[*],wget -O- -q $1/status | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$2}'
UserParameter=nginx.requests[*],wget -O- -q $1/status | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$3}'

Woohoo! You on a right way! Create template and items inside like this one

Create new graphic, all loves graphics!

Ok, you created cool monitoring for your Nginx, now look how to calculate rate in Zabbix.

Нет комментариев »

Яндекс.Метрика

Fortune cookie: Gravity is an unforgiving motherfucker.