1. 程式人生 > >zabbix-3.0TLS入門筆記

zabbix-3.0TLS入門筆記

zabbix dingtalk 報警

zabbix-3.0TLS部署

1.1 環境

os版本:centos6.8

版本:zabbix-3.0.15

PHP 5.6.34

Nginx-1.8.1

1.2安裝Nginx

tar -zxf nginx-1.8.1.tar.gz && cd nginx-1.8.1 && ./configure --prefix=/usr/local/nginx-1.8.1 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module && make && make install && ln -s /usr/local/nginx-1.8.1/ /usr/local/nginx && chown -R www:www /usr/local/nginx/

1.3安裝php

php

zxf php-5.6.34.tar.gz && cd php-5.6.34 && ./configure --prefix=/usr/local/php5.6.34 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp && make && make install && ln -s /usr/local/php5.6.34/ /usr/local/php

1.4安裝zabbix

yum install net-snmp-devel mysql-devel

編譯安裝

tar -zxf zabbix-3.0.15.tar.gz

make && make install

./configure --prefix=/usr/local/zabbix-3.0.15/ --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2

make && make install

ln -s /usr/local/zabbix-3.0.15/ /usr/local/zabbix-server

1.5創建數據庫

create database zabbix default character set utf8;

導入sql

mysql -uzabbix -pcentos -h 192.168.100.3 zabbix < /opt/zabbix-3.0.15/database/mysql/schema.sql #代理只要導入此sql

mysql -uzabbix -pcentos -h 192.168.100.3 zabbix < /opt/zabbix-3.0.15/database/mysql/images.sql

mysql -uzabbix -pcentos -h 192.168.100.3 zabbix < /opt/zabbix-3.0.15/database/mysql/data.sql

1.6修改各類配置文件參數

修改配置文件zabbix_server.conf中關於數據庫的參數

LogFile=/var/log/zabbix_server.log

LogFile=/usr/local/zabbix-server/log/zabbix_server.log

DBHost=192.168.100.3

DBName=zabbix

DBUser=zabbix

DBPassword=centos

Timeout=4

LogSlowQueries=3000

配置nginx虛擬主機

mkdir /usr/local/zabbix-server/data

mkdir /usr/local/zabbix-server/data

cp -rp frontends/php/* /usr/local/zabbix-server/data/

chown -R www:www /usr/local/zabbix-server/data/

nginx主配置文件nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

sendfile on;

keepalive_timeout 65;

include extra_conf/*;

}

虛擬主機配置文件

cat extra_conf/zabbix.conf

server {

listen 80;

server_name monitor.ttlsa.com;

access_log /var/log/zabbix.access.log main;

index index.html index.php index.html;

root /usr/local/zabbix-server/data/;

location /

{

try_files $uri $uri/ /index.php?$args;

}

location ~ ^(.+.php)(.*)$ {

fastcgi_split_path_info ^(.+.php)(.*)$;

include fastcgi.conf;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param PATH_INFO $fastcg

}

}



修改php參數,這是zabbixphp的硬性參數

373 max_execution_time = 300

383 max_input_time = 300

661 post_max_size = 16M

808 date.timezone = "Asia/Shanghai"


1.7web界面配置

技術分享圖片


技術分享圖片

技術分享圖片

技術分享圖片



技術分享圖片



技術分享圖片



1.8錯誤案例集

php參數修改未生效

修改/usr/local/php/etc/php.ini參數,重啟php沒有生效

php.ini存放位置錯誤

根據phpinfo查到configure配置文件目錄

技術分享圖片

※動態安裝php模塊 gettext ldap

web安裝時報錯

技術分享圖片

解決辦法

cd /opt/php-5.6.34

cd ext/gettext/

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php5.6.34/bin/php-config

make -j 6 && make install

ls /usr/local/php5.6.34/lib/php/extensions/no-debug-non-zts-20131226/

yum install –y openldap openldap-devel

ls /usr/lib64/libldap*

cp -rp /usr/lib64/libldap* /usr/lib

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php5.6.34/bin/php-config

make -j 6 && make install

ls /usr/local/php5.6.34/lib/php/extensions/no-debug-non-zts-20131226/

在配置文件php.ini添加如下

extension=gettext.so

extension=ldap.so

數據庫驗證錯誤

現象

The frontend does not match Zabbix database. Current database version (mandatory/optional): 2050061/2050061. Required mandatory version: 3000000. Contact your system administrator.

解決方案

刪除原有用戶及庫名,重新創建數據庫及用戶名,重新導入數據



二、zabbix實戰

2.1 zabbix監控nginx性能

首先開啟nginx stub_status模塊,server虛擬主機標簽中 location標簽中添加

location status

stub_status on;

access_log off;

allow 192.168.100.0/24;

deny all;

編寫數據采集腳本

實現方法主要是通過訪問nginx status來獲取相關數據。

cat scripts/nginx_status.sh

#!/bin/bash

#author: hudaohong

# nginx_status check

host=192.168.100.250

function ping {

/sbin/pidof nginx | wc -l

}

# check status

function active {

curl "http://$host/status" 2>/dev/null | grep "Active" | awk '{print $NF}'

}

function reading {

curl "http://$host/status" 2>/dev/null | grep "Reading" | awk '{print $2}'

}

function writing {

curl "http://$host/status" 2>/dev/null | grep "Writing" | awk '{print $4}'

}

function waiting {

curl "http://$host/status" 2>/dev/null | grep "Waiting" | awk '{print $6}'

}

function accepts {

curl "http://$host/status" 2>/dev/null | awk NR==3'{print $1}'

}

function handled {

curl "http://$host/status" 2>/dev/null | awk NR==3'{print $2}'

}

function requests {

curl "http://$host/status" 2>/dev/null | awk NR==3'{print $3}'

}

$1

修改zabbix agent端配置文件conf/zabbix_agentd.conf

Include=/usr/local/zabbix_agent/conf/zabbix_agentd/*.conf

增加配置文件userparameter_nginx.conf

cat conf/zabbix_agentd/userparameter_nginx.conf

# nginx status check

#zabbix_agent端定義key

UserParameter=nginx.status[*],/usr/local/zabbix_agent/scripts/nginx_status.sh $1

重啟agentd程序使配置生效

web上導入配置好的模版

nginx_status

讓主機關聯模版

技術分享圖片



三、zabbix自定義釘釘機器人報警

3.1註冊釘釘,為群會話添加機器人

通過pc版註冊釘釘

創建釘釘群會話

群設置-機器人

結果圖如下

技術分享圖片


3.2 zabbix添加-自定義的報警媒介

配置 AlertScriptsPath

grep --color AlertScriptsPath /usr/local/zabbix-server/etc/zabbix_server.conf

### Option: AlertScriptsPath

AlertScriptsPath=/usr/local/zabbix-server/share/zabbix/alertscripts

zabbix web添加自定義的報警媒介

管理-創建媒體類型

技術分享圖片

編寫消息發送腳本

#!/usr/bin/python

# -*- coding:utf-8 -*-

#author: hudahong

#mail: [email protected]

#descriptions: ding Talk robot send messaged!

#python version:2.7

#

import json

import urllib2

import sys

def ding_robot_send_msg(msg):

robot_api_url = 'https://oapi.dingtalk.com/robot/send?access_token=3e539964d3cd2445628758dfa4eb4863e382948dd91f7b32d188c2032bba5b'

headers_info = {

"Content-Type": "application/json ;charset=utf-8 "

}

String_textMsg = {

"msgtype": "text",

"text": {"content": msg}

}

String_textMsg = json.dumps(String_textMsg)

req = urllib2.Request(robot_api_url, data=String_textMsg, headers=headers_info)

urllib2.urlopen(req)

if __name__ == "__main__":

msg=sys.argv[1]

ding_robot_send_msg(msg)

此腳本需要優化:增加異常處理;增加日誌功能

修改版本:

cat dingTalkRobot.py

#!/usr/bin/python

# -*- coding:utf-8 -*-

#author: hudahong

#mail: [email protected]

#descriptions: ding Talk robot send messaged!

#python version:2.7

#

import json

import urllib2

import sys

import logging

def ding_robot_send_msg(msg):

# 定義日誌模塊

logging.basicConfig(filename="/usr/local/zabbix-server/log/dingrobot.log",

format='%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(message)s',

datefmt='%Y-%m-%d %H:%M:%S',

level=10,

filemode='a',

)

robot_api_url = 'https://oapi.dingtalk.com/robot/send?access_token=3e539964d3cd2445628758dfa4eb4863e382948dd91f7b32d188c2032bba5b'

headers_info = {

"Content-Type": "application/json ;charset=utf-8 "

}

String_textMsg = {

"msgtype": "text",

"text": {"content": msg}

}

String_textMsg = json.dumps(String_textMsg)

try:

req = urllib2.Request(robot_api_url, data=String_textMsg, headers=headers_info)

logging.info(urllib2.urlopen(req).read().decode())

except (urllib2.URLError) as E:

print("Error:",E)

logging.error(E)

if __name__ == "__main__":

msg=sys.argv[1]

ding_robot_send_msg(msg)

zabbix用戶指定媒介

管理-用戶-Amin-報警媒介-添加

技術分享圖片

3.3 配置zabbix報警之action

創建action

創建動作有四個事件來源,它們分別是觸發器 自動發現 自動註冊 事件源

配置-動作-創建創建動作 技術分享圖片


技術分享圖片


默認信息

{

服務器:{HOST.NAME}發生故障: {TRIGGER.NAME}!

告警主機:{HOST.NAME}

告警地址:{HOST.IP}

監控項目:{ITEM.NAME}

監控取值:{ITEM.LASTVALUE}

告警等級:{TRIGGER.SEVERITY}

當前狀態:{TRIGGER.STATUS}

告警信息:{TRIGGER.NAME}

告警時間:{EVENT.DATE} {EVENT.TIME}

事件ID:{EVENT.ID}

}

恢復信息

{

服務器:{HOST.NAME} {TRIGGER.NAME}故障已恢復!

告警主機:{HOST.NAME}

告警地址:{HOST.IP}

監控項目:{ITEM.NAME}

監控取值:{ITEM.LASTVALUE}

告警等級:{TRIGGER.SEVERITY}

當前狀態:{TRIGGER.STATUS}

告警信息:{TRIGGER.NAME}

告警時間:{EVENT.DATE} {EVENT.TIME}

恢復時間:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}

持續時間:{EVENT.AGE}

事件ID:{EVENT.ID}

}

配置條件

技術分享圖片


在配置條件時選擇主機來指定給特定主機發送報警消息

技術分享圖片

配置動作

技術分享圖片

模擬故障

server3.example.comnginx服務關閉

結果信息如下

技術分享圖片







zabbix-3.0TLS入門筆記