INT 21h

Hi, I am Vladimir Smagin, SysAdmin, DevOps and barely good guy. Telegram Email / GIT / Микроблог / Thingiverse / GPG

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.

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

Система мониторинга Zabbix

№ 8101. В разделе " Sysadmin " от February 26th, 2016

В подшивках: , ,

Микроблог перейти

# 2019-12-23 09:14:01

Очень интересная концепция, docker-compose для kubernetes kompose.io/ #devops #kubernetes #docker

# 2019-12-16 17:21:18

Надо будет почитать для общего развития как оживить кластер в случае проеба сертов habr.com/ru/company/southbridg #kubernetes #devops

# 2019-12-12 17:33:46

js bootstrap с интерфейсом как в DOS github.com/kristopolous/BOOTST


© Vladimir Smagin, 2005-2019. Копирование материалов без разрешения запрещено. GPG DA4CD0F5E222EA727D6A40C413BCE12E5618F071 *
Яндекс.Метрика

Fortune cookie: Today's spam: Your device is so tiny she hardly finds it in bed?