Skip to content

Instantly share code, notes, and snippets.

@kapb14
Last active December 20, 2021 20:12
Show Gist options
  • Save kapb14/9b58d361fb5758df897929459c3655a8 to your computer and use it in GitHub Desktop.
Save kapb14/9b58d361fb5758df897929459c3655a8 to your computer and use it in GitHub Desktop.
Nginx HTTP status code monitoring with Zabbix and Lua
  1. нужен LUA модуль для Nginx, он есть в репах Debian

apt-get install nginx-extras

  1. используется lua скрипт для мониторинга Nginx при помощи prometheus (а че - он удобный вполне)
mkdir /etc/nginx/lua
git clone https://github.com/knyar/nginx-lua-prometheus /etc/nginx/lua/nginx-lua-prometheus
  1. добавиляем в Nginx конфиг нового виртуалхоста "nginx_status.conf"

  2. кладём куда-нибудь в /etc/zabbix скрипт "nginx-httpstatus.sh"

  3. в crontab пользователя "zabbix" задаём выполнение скрипта "nginx-httpstatus.sh" раз в минуту

  4. импортируем в Zabbix шаблон "nginx_httpstatus_zbx_template.xml"

#!/bin/bash
# crontab -u zabbix -e
#* * * * * zabbix bash /etc/zabbix/nginx-http-status.sh
VERBOSE=false
for arg in $@; do
cnt=0
if [[ "${arg}" == "-v" ]]; then
(( cnt+=1 ))
VERBOSE=true
else
arg_host=${arg}
fi
done
lua_host="127.0.0.1:81"
lua_location="metrics"
url="http://${lua_host}/${lua_location}"
host=${arg_host:-www.site.ru}
tmpfile=$(/bin/mktemp /tmp/$(basename $0).tmp.XXXXXXXXX)
trap "{ /bin/rm -f $tmpfile ; exit 255; }" EXIT
send_status(){
code=$1
key_name="http${code}"
code_status=$(grep "status=\"${code}\"" ${tmpfile} | rev | cut -d' ' -f1 | rev)
if [[ -z "${code_status}" ]]; then
code_status=0
fi
if ${VERBOSE}; then
printf "%s%10s\n" "${code}" "${code_status}"
fi
/usr/bin/zabbix_sender --config /etc/zabbix/zabbix_agentd.conf --key "${key_name}" --value "${code_status}" 2>&1 >/dev/null
}
${VERBOSE} && echo "host: ${host}"
curl -s ${url} | grep "nginx_http_requests_total" | grep "host=\"${host}\"" > ${tmpfile}
${VERBOSE} && cat ${tmpfile} > /tmp/nginx-http-status.tmp && echo "tmpfile: /tmp/nginx-http-status.tmp"
${VERBOSE} && printf "%s%10s\n" "[code]" "[status]"
for code in 200 301 302 400 401 403 404 429 499 500 501 502 503 504; do
send_status ${code}
done
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.2</version>
<date>2018-06-18T18:43:50Z</date>
<groups>
<group>
<name>!Templates</name>
</group>
</groups>
<templates>
<template>
<template>nginx lua status</template>
<name>nginx lua status</name>
<description/>
<groups>
<group>
<name>!Templates</name>
</group>
</groups>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<items>
<item>
<name>nginx http code 200</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http200</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 401</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http401</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 403</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http403</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 404</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http404</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 500</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http500</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 501</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http501</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 502</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http502</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 503</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http503</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx http code 504</name>
<type>2</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>http504</key>
<delay>0</delay>
<history>30</history>
<trends>60</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>2</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
</items>
<discovery_rules/>
<httptests/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
<graphs>
<graph>
<name>Nginx http status codes</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http200</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>0</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http401</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>0</drawtype>
<color>2774A4</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http403</key>
</item>
</graph_item>
<graph_item>
<sortorder>3</sortorder>
<drawtype>0</drawtype>
<color>A54F10</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http404</key>
</item>
</graph_item>
<graph_item>
<sortorder>4</sortorder>
<drawtype>0</drawtype>
<color>FC6EA3</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http500</key>
</item>
</graph_item>
<graph_item>
<sortorder>5</sortorder>
<drawtype>0</drawtype>
<color>6C59DC</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http501</key>
</item>
</graph_item>
<graph_item>
<sortorder>6</sortorder>
<drawtype>0</drawtype>
<color>AC8C14</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http502</key>
</item>
</graph_item>
<graph_item>
<sortorder>7</sortorder>
<drawtype>0</drawtype>
<color>611F27</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http503</key>
</item>
</graph_item>
<graph_item>
<sortorder>8</sortorder>
<drawtype>0</drawtype>
<color>F230E0</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>nginx lua status</host>
<key>http504</key>
</item>
</graph_item>
</graph_items>
</graph>
</graphs>
</zabbix_export>
lua_shared_dict prometheus_metrics 10M;
lua_package_path "/etc/nginx/lua/nginx-lua-prometheus/?.lua";
init_by_lua '
prometheus = require("prometheus").init("prometheus_metrics")
metric_requests = prometheus:counter(
"nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
metric_latency = prometheus:histogram(
"nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
';
log_by_lua '
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
';
server {
listen 127.0.0.1:81 default_server;
#listen 81;
server_name localhost;
root /usr/share/nginx/html;
charset utf-8;
location /metrics {
content_by_lua '
metric_connections:set(ngx.var.connections_reading, {"reading"})
metric_connections:set(ngx.var.connections_waiting, {"waiting"})
metric_connections:set(ngx.var.connections_writing, {"writing"})
prometheus:collect()
';
}
location /nginx_status {
stub_status on;
access_log off;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment