• zabbix 2.4.5 自定义key监控nginx


    一、环境

    ubuntu 14.04 LTS

    zabbix 2.4.5

    nginx/1.4.6

    二、配置

    1、确定Nginx HttpStubStatusModule模块已经安装,可以通过nginx -V来查看

    nginx -V
    
    nginx version: nginx/1.4.6 (Ubuntu)
    built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1) 
    TLS SNI support enabled
    configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module

    2、配置nginx文件

    cd  /etc/nginx/conf.d
    sudo vim zabbix-monitor-nginx.conf
    
    #Add the following into Nginx configuration:
    
    server {
        listen 10061;
        location /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
        }
    }
    
    # reload nginx  
    sudo nginx -t && sudo nginx -s reload

    3、测试nginx配置是否成功`curl http://127.0.0.1:10061/nginx_status` 获取数据

    curl http://127.0.0.1:10061/nginx_status
    
    Active connections: 1 
    server accepts handled requests
     167 167 167 
    Reading: 0 Writing: 1 Waiting: 0 

    4、编写nginx status检测脚本

    cd  /etc/zabbix
    sudo mkdir scripts
    sudo vim scripts/nginx-check.sh
    
    #Add the following bash codes
    
    #!/bin/bash
    ##################################
    # Zabbix monitoring script
    #
    # nginx:
    #  - anything available via nginx stub-status module
    #
    ##################################
    # Contact:
    #  vincent.viallet@gmail.com
    ##################################
    # ChangeLog:
    #  20100922    VV    initial creation
    ##################################
    
    # Zabbix requested parameter
    ZBX_REQ_DATA="$1"
    ZBX_REQ_DATA_URL="$2"
    
    # Nginx defaults
    NGINX_STATUS_DEFAULT_URL="http://localhost:80/nginx_status"
    WGET_BIN="/usr/bin/wget"
    
    #
    # Error handling:
    #  - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
    #  - items need to be of type "float" (allow negative + float)
    #
    ERROR_NO_ACCESS_FILE="-0.9900"
    ERROR_NO_ACCESS="-0.9901"
    ERROR_WRONG_PARAM="-0.9902"
    ERROR_DATA="-0.9903" # either can not connect /    bad host / bad port
    
    # Handle host and port if non-default
    if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
      URL="$ZBX_REQ_DATA_URL"
    else
      URL="$NGINX_STATUS_DEFAULT_URL"
    fi
    
    # save the nginx stats in a variable for future parsing
    NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
    
    # error during retrieve
    if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
      echo $ERROR_DATA
      exit 1
    fi
    
    # 
    # Extract data from nginx stats
    #
    case $ZBX_REQ_DATA in
      active_connections)   echo "$NGINX_STATS" | head -1             | cut -f3 -d' ';;
      accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';;
      handled_connections)  echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';;
      handled_requests)     echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';;
      reading)              echo "$NGINX_STATS" | tail -1             | cut -f2 -d' ';;
      writing)              echo "$NGINX_STATS" | tail -1             | cut -f4 -d' ';;
      waiting)              echo "$NGINX_STATS" | tail -1             | cut -f6 -d' ';;
      *) echo $ERROR_WRONG_PARAM; exit 1;;
    esac
    
    exit 0

    sudo chown -R zabbix:zabbix ./scripts/
    sudo chmod +x nginx-check.sh

    5、修改zabbix_agent.conf

    sudo vi /etc/zabbix/zabbix_agent.conf

    ###Change the value of UnsafeUserParameters
    =1 ### Add User Parameters UserParameter=nginx[*],/etc/zabbix/scripts/nginx-check.sh "$1" "$2"

    sudo service zabbix-agent restart

    6、导入Template nginx

      Import nginx-template.xml, and link it to a host. Set the host macro {$NGINX_STATUS_URL} if needed.

      nginx模板

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <zabbix_export>
      3     <version>2.0</version>
      4     <date>2015-06-19T15:17:58Z</date>
      5     <groups>
      6         <group>
      7             <name>Templates</name>
      8         </group>
      9     </groups>
     10     <templates>
     11         <template>
     12             <template>Template_Nginx</template>
     13             <name>Template_Nginx</name>
     14             <groups>
     15                 <group>
     16                     <name>Templates</name>
     17                 </group>
     18             </groups>
     19             <applications>
     20                 <application>
     21                     <name>Nginx</name>
     22                 </application>
     23             </applications>
     24             <items>
     25                 <item>
     26                     <name>Nginx $1</name>
     27                     <type>0</type>
     28                     <snmp_community/>
     29                     <multiplier>0</multiplier>
     30                     <snmp_oid/>
     31                     <key>nginx[waiting,{$NGINX_STATUS_URL}]</key>
     32                     <delay>60</delay>
     33                     <history>30</history>
     34                     <trends>365</trends>
     35                     <status>0</status>
     36                     <value_type>0</value_type>
     37                     <allowed_hosts/>
     38                     <units/>
     39                     <delta>0</delta>
     40                     <snmpv3_securityname/>
     41                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
     42                     <snmpv3_authpassphrase/>
     43                     <snmpv3_privpassphrase/>
     44                     <formula>1</formula>
     45                     <delay_flex/>
     46                     <params/>
     47                     <ipmi_sensor/>
     48                     <data_type>0</data_type>
     49                     <authtype>0</authtype>
     50                     <username/>
     51                     <password/>
     52                     <publickey/>
     53                     <privatekey/>
     54                     <port/>
     55                     <description/>
     56                     <inventory_link>0</inventory_link>
     57                     <applications>
     58                         <application>
     59                             <name>Nginx</name>
     60                         </application>
     61                     </applications>
     62                     <valuemap/>
     63                 </item>
     64                 <item>
     65                     <name>Nginx $1</name>
     66                     <type>0</type>
     67                     <snmp_community/>
     68                     <multiplier>0</multiplier>
     69                     <snmp_oid/>
     70                     <key>nginx[writing,{$NGINX_STATUS_URL}]</key>
     71                     <delay>60</delay>
     72                     <history>30</history>
     73                     <trends>365</trends>
     74                     <status>0</status>
     75                     <value_type>0</value_type>
     76                     <allowed_hosts/>
     77                     <units/>
     78                     <delta>0</delta>
     79                     <snmpv3_securityname/>
     80                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
     81                     <snmpv3_authpassphrase/>
     82                     <snmpv3_privpassphrase/>
     83                     <formula>1</formula>
     84                     <delay_flex/>
     85                     <params/>
     86                     <ipmi_sensor/>
     87                     <data_type>0</data_type>
     88                     <authtype>0</authtype>
     89                     <username/>
     90                     <password/>
     91                     <publickey/>
     92                     <privatekey/>
     93                     <port/>
     94                     <description/>
     95                     <inventory_link>0</inventory_link>
     96                     <applications>
     97                         <application>
     98                             <name>Nginx</name>
     99                         </application>
    100                     </applications>
    101                     <valuemap/>
    102                 </item>
    103                 <item>
    104                     <name>Nginx $1</name>
    105                     <type>0</type>
    106                     <snmp_community/>
    107                     <multiplier>0</multiplier>
    108                     <snmp_oid/>
    109                     <key>nginx[active_connections,{$NGINX_STATUS_URL}]</key>
    110                     <delay>60</delay>
    111                     <history>30</history>
    112                     <trends>365</trends>
    113                     <status>0</status>
    114                     <value_type>0</value_type>
    115                     <allowed_hosts/>
    116                     <units/>
    117                     <delta>0</delta>
    118                     <snmpv3_securityname/>
    119                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
    120                     <snmpv3_authpassphrase/>
    121                     <snmpv3_privpassphrase/>
    122                     <formula>1</formula>
    123                     <delay_flex/>
    124                     <params/>
    125                     <ipmi_sensor/>
    126                     <data_type>0</data_type>
    127                     <authtype>0</authtype>
    128                     <username/>
    129                     <password/>
    130                     <publickey/>
    131                     <privatekey/>
    132                     <port/>
    133                     <description/>
    134                     <inventory_link>0</inventory_link>
    135                     <applications>
    136                         <application>
    137                             <name>Nginx</name>
    138                         </application>
    139                     </applications>
    140                     <valuemap/>
    141                 </item>
    142                 <item>
    143                     <name>Nginx $1</name>
    144                     <type>0</type>
    145                     <snmp_community/>
    146                     <multiplier>0</multiplier>
    147                     <snmp_oid/>
    148                     <key>nginx[reading,{$NGINX_STATUS_URL}]</key>
    149                     <delay>60</delay>
    150                     <history>30</history>
    151                     <trends>365</trends>
    152                     <status>0</status>
    153                     <value_type>0</value_type>
    154                     <allowed_hosts/>
    155                     <units/>
    156                     <delta>0</delta>
    157                     <snmpv3_securityname/>
    158                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
    159                     <snmpv3_authpassphrase/>
    160                     <snmpv3_privpassphrase/>
    161                     <formula>1</formula>
    162                     <delay_flex/>
    163                     <params/>
    164                     <ipmi_sensor/>
    165                     <data_type>0</data_type>
    166                     <authtype>0</authtype>
    167                     <username/>
    168                     <password/>
    169                     <publickey/>
    170                     <privatekey/>
    171                     <port/>
    172                     <description/>
    173                     <inventory_link>0</inventory_link>
    174                     <applications>
    175                         <application>
    176                             <name>Nginx</name>
    177                         </application>
    178                     </applications>
    179                     <valuemap/>
    180                 </item>
    181                 <item>
    182                     <name>Nginx $1/sec</name>
    183                     <type>0</type>
    184                     <snmp_community/>
    185                     <multiplier>0</multiplier>
    186                     <snmp_oid/>
    187                     <key>nginx[handled_requests,{$NGINX_STATUS_URL}]</key>
    188                     <delay>60</delay>
    189                     <history>30</history>
    190                     <trends>365</trends>
    191                     <status>0</status>
    192                     <value_type>0</value_type>
    193                     <allowed_hosts/>
    194                     <units/>
    195                     <delta>1</delta>
    196                     <snmpv3_securityname/>
    197                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
    198                     <snmpv3_authpassphrase/>
    199                     <snmpv3_privpassphrase/>
    200                     <formula>1</formula>
    201                     <delay_flex/>
    202                     <params/>
    203                     <ipmi_sensor/>
    204                     <data_type>0</data_type>
    205                     <authtype>0</authtype>
    206                     <username/>
    207                     <password/>
    208                     <publickey/>
    209                     <privatekey/>
    210                     <port/>
    211                     <description/>
    212                     <inventory_link>0</inventory_link>
    213                     <applications>
    214                         <application>
    215                             <name>Nginx</name>
    216                         </application>
    217                     </applications>
    218                     <valuemap/>
    219                 </item>
    220                 <item>
    221                     <name>Nginx $1/sec</name>
    222                     <type>0</type>
    223                     <snmp_community/>
    224                     <multiplier>0</multiplier>
    225                     <snmp_oid/>
    226                     <key>nginx[accepted_connections,{$NGINX_STATUS_URL}]</key>
    227                     <delay>60</delay>
    228                     <history>30</history>
    229                     <trends>365</trends>
    230                     <status>0</status>
    231                     <value_type>0</value_type>
    232                     <allowed_hosts/>
    233                     <units/>
    234                     <delta>1</delta>
    235                     <snmpv3_securityname/>
    236                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
    237                     <snmpv3_authpassphrase/>
    238                     <snmpv3_privpassphrase/>
    239                     <formula>1</formula>
    240                     <delay_flex/>
    241                     <params/>
    242                     <ipmi_sensor/>
    243                     <data_type>0</data_type>
    244                     <authtype>0</authtype>
    245                     <username/>
    246                     <password/>
    247                     <publickey/>
    248                     <privatekey/>
    249                     <port/>
    250                     <description/>
    251                     <inventory_link>0</inventory_link>
    252                     <applications>
    253                         <application>
    254                             <name>Nginx</name>
    255                         </application>
    256                     </applications>
    257                     <valuemap/>
    258                 </item>
    259                 <item>
    260                     <name>Nginx $1/sec</name>
    261                     <type>0</type>
    262                     <snmp_community/>
    263                     <multiplier>0</multiplier>
    264                     <snmp_oid/>
    265                     <key>nginx[handled_connections,{$NGINX_STATUS_URL}]</key>
    266                     <delay>60</delay>
    267                     <history>30</history>
    268                     <trends>365</trends>
    269                     <status>0</status>
    270                     <value_type>0</value_type>
    271                     <allowed_hosts/>
    272                     <units/>
    273                     <delta>1</delta>
    274                     <snmpv3_securityname/>
    275                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
    276                     <snmpv3_authpassphrase/>
    277                     <snmpv3_privpassphrase/>
    278                     <formula>1</formula>
    279                     <delay_flex/>
    280                     <params/>
    281                     <ipmi_sensor/>
    282                     <data_type>0</data_type>
    283                     <authtype>0</authtype>
    284                     <username/>
    285                     <password/>
    286                     <publickey/>
    287                     <privatekey/>
    288                     <port/>
    289                     <description/>
    290                     <inventory_link>0</inventory_link>
    291                     <applications>
    292                         <application>
    293                             <name>Nginx</name>
    294                         </application>
    295                     </applications>
    296                     <valuemap/>
    297                 </item>
    298                 <item>
    299                     <name>Number of $1 process</name>
    300                     <type>0</type>
    301                     <snmp_community>public</snmp_community>
    302                     <multiplier>0</multiplier>
    303                     <snmp_oid>interfaces.ifTable.ifEntry.ifInOctets.1</snmp_oid>
    304                     <key>proc.num[nginx]</key>
    305                     <delay>300</delay>
    306                     <history>30</history>
    307                     <trends>365</trends>
    308                     <status>0</status>
    309                     <value_type>3</value_type>
    310                     <allowed_hosts/>
    311                     <units/>
    312                     <delta>0</delta>
    313                     <snmpv3_securityname/>
    314                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
    315                     <snmpv3_authpassphrase/>
    316                     <snmpv3_privpassphrase/>
    317                     <formula>1</formula>
    318                     <delay_flex/>
    319                     <params/>
    320                     <ipmi_sensor/>
    321                     <data_type>0</data_type>
    322                     <authtype>0</authtype>
    323                     <username/>
    324                     <password/>
    325                     <publickey/>
    326                     <privatekey/>
    327                     <port/>
    328                     <description/>
    329                     <inventory_link>0</inventory_link>
    330                     <applications/>
    331                     <valuemap/>
    332                 </item>
    333             </items>
    334             <discovery_rules/>
    335             <macros>
    336                 <macro>
    337                     <macro>{$NGINX_STATUS_URL}</macro>
    338                     <value>http://127.0.0.1:10061/nginx_status</value>
    339                 </macro>
    340             </macros>
    341             <templates/>
    342             <screens/>
    343         </template>
    344     </templates>
    345     <triggers>
    346         <trigger>
    347             <expression>{Template_Nginx:proc.num[nginx].last(0)}=0</expression>
    348             <name>Nginx is not running on {HOSTNAME}</name>
    349             <url/>
    350             <status>0</status>
    351             <priority>4</priority>
    352             <description>Nginx is not running.&#13;
    353       &#13;
    354       It has been stopped / shutdown or has crashed. &#13;
    355       Check on the server for more details:&#13;
    356         - w / last&#13;
    357         - dmesg logs&#13;
    358         - /var/log/messages&#13;
    359         - nginx error logs</description>
    360             <type>0</type>
    361             <dependencies/>
    362         </trigger>
    363     </triggers>
    364     <graphs>
    365         <graph>
    366             <name>Nginx - Connections and Requests status</name>
    367             <width>900</width>
    368             <height>200</height>
    369             <yaxismin>0.0000</yaxismin>
    370             <yaxismax>100.0000</yaxismax>
    371             <show_work_period>0</show_work_period>
    372             <show_triggers>0</show_triggers>
    373             <type>0</type>
    374             <show_legend>1</show_legend>
    375             <show_3d>0</show_3d>
    376             <percent_left>0.0000</percent_left>
    377             <percent_right>0.0000</percent_right>
    378             <ymin_type_1>1</ymin_type_1>
    379             <ymax_type_1>0</ymax_type_1>
    380             <ymin_item_1>0</ymin_item_1>
    381             <ymax_item_1>0</ymax_item_1>
    382             <graph_items>
    383                 <graph_item>
    384                     <sortorder>0</sortorder>
    385                     <drawtype>1</drawtype>
    386                     <color>FF9999</color>
    387                     <yaxisside>0</yaxisside>
    388                     <calc_fnc>4</calc_fnc>
    389                     <type>0</type>
    390                     <item>
    391                         <host>Template_Nginx</host>
    392                         <key>nginx[accepted_connections,{$NGINX_STATUS_URL}]</key>
    393                     </item>
    394                 </graph_item>
    395                 <graph_item>
    396                     <sortorder>1</sortorder>
    397                     <drawtype>2</drawtype>
    398                     <color>990000</color>
    399                     <yaxisside>0</yaxisside>
    400                     <calc_fnc>4</calc_fnc>
    401                     <type>0</type>
    402                     <item>
    403                         <host>Template_Nginx</host>
    404                         <key>nginx[handled_connections,{$NGINX_STATUS_URL}]</key>
    405                     </item>
    406                 </graph_item>
    407                 <graph_item>
    408                     <sortorder>2</sortorder>
    409                     <drawtype>0</drawtype>
    410                     <color>009900</color>
    411                     <yaxisside>0</yaxisside>
    412                     <calc_fnc>4</calc_fnc>
    413                     <type>0</type>
    414                     <item>
    415                         <host>Template_Nginx</host>
    416                         <key>nginx[handled_requests,{$NGINX_STATUS_URL}]</key>
    417                     </item>
    418                 </graph_item>
    419             </graph_items>
    420         </graph>
    421         <graph>
    422             <name>Nginx - Threads status</name>
    423             <width>900</width>
    424             <height>200</height>
    425             <yaxismin>0.0000</yaxismin>
    426             <yaxismax>100.0000</yaxismax>
    427             <show_work_period>0</show_work_period>
    428             <show_triggers>0</show_triggers>
    429             <type>1</type>
    430             <show_legend>1</show_legend>
    431             <show_3d>0</show_3d>
    432             <percent_left>0.0000</percent_left>
    433             <percent_right>0.0000</percent_right>
    434             <ymin_type_1>1</ymin_type_1>
    435             <ymax_type_1>0</ymax_type_1>
    436             <ymin_item_1>0</ymin_item_1>
    437             <ymax_item_1>0</ymax_item_1>
    438             <graph_items>
    439                 <graph_item>
    440                     <sortorder>0</sortorder>
    441                     <drawtype>1</drawtype>
    442                     <color>990000</color>
    443                     <yaxisside>0</yaxisside>
    444                     <calc_fnc>4</calc_fnc>
    445                     <type>0</type>
    446                     <item>
    447                         <host>Template_Nginx</host>
    448                         <key>nginx[writing,{$NGINX_STATUS_URL}]</key>
    449                     </item>
    450                 </graph_item>
    451                 <graph_item>
    452                     <sortorder>1</sortorder>
    453                     <drawtype>1</drawtype>
    454                     <color>999900</color>
    455                     <yaxisside>0</yaxisside>
    456                     <calc_fnc>4</calc_fnc>
    457                     <type>0</type>
    458                     <item>
    459                         <host>Template_Nginx</host>
    460                         <key>nginx[reading,{$NGINX_STATUS_URL}]</key>
    461                     </item>
    462                 </graph_item>
    463                 <graph_item>
    464                     <sortorder>2</sortorder>
    465                     <drawtype>1</drawtype>
    466                     <color>009900</color>
    467                     <yaxisside>0</yaxisside>
    468                     <calc_fnc>4</calc_fnc>
    469                     <type>0</type>
    470                     <item>
    471                         <host>Template_Nginx</host>
    472                         <key>nginx[waiting,{$NGINX_STATUS_URL}]</key>
    473                     </item>
    474                 </graph_item>
    475             </graph_items>
    476         </graph>
    477     </graphs>
    478 </zabbix_export>

     参考资料:https://github.com/jizhang

  • 相关阅读:
    关于Python对文件字节流数据的处理
    python中的random模块
    软件开发项目验收标准
    pdf文档转图片
    批量处理图片转base64编码
    批量处理图片转为透明色
    python2.7实现本地启一个http服务作为数据转发接收器或模拟接口响应数据源
    系统正常运行指标参考
    Jenkins创建一个自由风格的项目
    KatalonRecorder系列(一):基本使用+XPath元素定位
  • 原文地址:https://www.cnblogs.com/chrisDuan/p/4588854.html
Copyright © 2020-2023  润新知