且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Zabbix---添加linux服务器监控

更新时间:2022-03-06 21:52:52

一,首先配置防火墙

1,添加zabbix 服务端的端口,如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@AppServerB conf]# vi /etc/sysconfig/iptables
  
-A INPUT -p udp --dport 10051 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 18091 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10050:10051 -j ACCEPT
# Generated by iptables-save v1.4.7 on Fri May 22 16:42:47 2015
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [312404188:85274465529]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p udp --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p udp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -s 192.168.1.244 -m state --state NEW -m udp -p udp --dport 10050:10051 -j ACCEPT
-A INPUT -s 192.168.1.244 -m state --state NEW -m tcp -p tcp --dport 10050:10051 -j ACCEPT
(只添加192.168.1.244服务器的访问)


2,重启防火墙,使配置生效。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@AppServerB conf]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Flushing firewall rules: [  OK  ]
iptables: Unloading modules: [  OK  ]
iptables: Applying firewall rules: [  OK  ]
[root@AppServerB conf]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:123 state NEW,ESTABLISHED 
4    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:161 state NEW,ESTABLISHED 
5    ACCEPT     udp  --  192.168.1.244        0.0.0.0/0           state NEW udp dpts:10050:10051 
6    ACCEPT     tcp  --  192.168.1.244        0.0.0.0/0           state NEW tcp dpts:10050:10051


注意,如果防火墙关闭的话,以上步骤可以省略。

 

二,安装zabbix_agentd的步骤及方法:

方法一:直接从官网上下载安装

1.1,安装zabbix_agentd需要从官网上下载相对应的软件,http://www.zabbix.com/download2.php

我使用的是2.0.8

 

1.2,把下载的包上传到所加的linux服务器上。我上传的路径是/tmp/install/

1
2
3
4
[root@AppServerB zabbix_agent]# ll /tmp/install/
total 452
-rw-r--r--. 1 root root 178068 Aug 25 12:47 net-snmp-utils-5.5-49.el6_5.3.x86_64.rpm
-rw-r--r--. 1 root root 279646 Sep 11 13:10 zabbix_agents_2.0.8.linux2_6.amd64.tar.gz


1.3,创建zabbix用户和组:

1
2
[root@AppServerA conf]# groupadd zabbix
[root@AppServerA conf]# useradd -g zabbix zabbix


1.4,解压下载的软件包:

1
2
3
4
5
6
7
8
9
[root@AppServerB install]# tar zxvf zabbix_agents_2.0.8.linux2_6.amd64.tar.gz -C /usr/local/zabbix_agent
sbin/zabbix_agent
sbin/zabbix_agentd
bin/zabbix_get
bin/zabbix_sender
conf/zabbix_agent.conf
conf/zabbix_agentd.conf
conf/zabbix_agentd/userparameter_examples.conf
conf/zabbix_agentd/userparameter_mysql.conf

 

方法二:通源码编译安装,具体如下:

2.1,添加用户群组

1
2
3
4
[root@server98 zabbix-3.4.3]# groupadd zabbix
[root@server98 zabbix-3.4.3]# useradd -g zabbix zabbix
[root@server98 zabbix-3.4.3]# tail -1 /etc/passwd
zabbix:x:1000:1000::/home/zabbix:/bin/bash

2.2,解压安装包,并编译安装

1
2
3
4
root@server98 tmp]# tar xf zabbix-3.4.3.tar.gz 
[root@server98 tmp]# cd zabbix-3.4.3
[root@server98 zabbix-3.4.3]# ./configure --prefix=/usr/local/zabbix --enable-agent
[root@server98 zabbix-3.4.3]# make install


三,zabbix_agent相关配置

1,编辑解压出来的zabbix_agentd.conf文件,修改下面的参数:

1
2
3
4
5
6
Hostname=本机名  //在本机上用hostname查看的结果必须和服务端添加的相同
ServerActive=192.168.99.99   //服务端的ip
Server=Zabbix Server IP  //zabbix服务器的ip
LogFile=本机agentd日志保存文件
SourceIP=本机IP
ListenPort=10050

2,修改/etc/services增加服务器的端口号:

 

1
2
3
4
5
6
[root@AppServerB conf]# cat >> /etc/services 
zabbix-agent 10050/tcp Zabbix Agent
zabbix-agent 10050/udp Zabbix Agent
zabbix-trapper 10051/tcp Zabbix Trapper
zabbix-trapper 10051/udp Zabbix Trapper
EOF


Ctrl+c 直接退出。

 

3,启动客户端:

1
2
[root@AppServerB conf]#
 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agent


查看服务是否启动:

1
2
3
4
5
6
7
8
[root@AppServerB conf]# ps -ef|grep zabbix
zabbix    6640     1  0 13:45 ?        00:00:00 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
zabbix    6641  6640  0 13:45 ?        00:00:00 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
zabbix    6642  6640  0 13:45 ?        00:00:00 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
zabbix    6643  6640  0 13:45 ?        00:00:00 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
zabbix    6644  6640  0 13:45 ?        00:00:00 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
zabbix    6645  6640  0 13:45 ?        00:00:00 /usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
root      6659  3755  0 13:45 pts/0    00:00:00 grep zabbix


4,在服务端通过zabbix_agent工具进行和客户端进行测试。

1
2
[root@localhost ~]# zabbix_get -s192.168.1.250 -p10050 -k"system.uptime"  
1980619


返回数值,说明配置正确。

 

5,把zabbix_agentd配置成服务,把以下脚本考到/etc/init.d/zabbix_agentd下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@server98 zabbix-3.4.3]# cp /tmp/zabbix-3.4.3/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@server98 init.d]# chmod +x zabbix_agentd 
[root@server98 init.d]# ln -s /usr/local/zabbix/sbin/zabbix_agentd /usr/local/sbin/
[root@server98 init.d]# ln -s /usr/local/zabbix/bin/zabbix_* /usr/local/bin/
[root@server98 init.d]# chkconfig --add zabbix_agentd
[root@server98 init.d]# /etc/init.d/zabbix_agentd start
Starting zabbix_agentd (via systemctl):                    [  确定  ]
[root@server98 init.d]# ps -ef|grep zabbix
zabbix   29301     1  0 15:28 ?        00:00:00 /usr/local/sbin/zabbix_agentd
zabbix   29302 29301  0 15:28 ?        00:00:00 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix   29303 29301  0 15:28 ?        00:00:00 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix   29304 29301  0 15:28 ?        00:00:00 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix   29305 29301  0 15:28 ?        00:00:00 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix   29306 29301  0 15:28 ?        00:00:00 /usr/local/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root     29308 11763  0 15:28 pts/1    00:00:00 grep --color=auto zabbix


一下是备用脚本2.x版本的,3.x版本的详见附件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  
#!/bin/bash
#
# chkconfig: - 55 45
# description: zabbix_agentd  
# probe: false
  
# Source function library.
/etc/rc.d/init.d/functions
  
# Source networking configuration.
/etc/sysconfig/network
  
# Check that networking is up. If you are running without a network, comment this out.
"${NETWORKING}" "no" ] && exit 0
  
RETVAL=0
progdir="/usr/local/zabbix/sbin/"
prog="zabbix_agentd"
  
FULLPATH=$progdir/$prog
CONF_FILE="/usr/local/zabbix/conf/zabbix_agentd.conf"
action $"starting $prog: " $FULLPATH -c $CONF_FILE
  
start() {
        # Start daemons.
        if [ -n "`/sbin/pidof $prog`" ]; then
                echo -n "$prog: already running"
                failure $"$prog start"
                echo
                return 1
        fi
        echo -n $"Starting $prog: "
        # we can't seem to use daemon here - emulate its functionality
        su -c $progdir$prog - $USER 
        RETVAL=$?
        usleep 100000
        if [ -z "`/sbin/pidof $progdir$prog`" ]; then
                # The child processes have died after fork()ing, e.g.
                # because of a broken config file
                RETVAL=1
        fi
        [ $RETVAL -ne 0 ] && failure $"$prog startup"
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog && success $"$prog startup"
        echo
        return $RETVAL
}
stop() {
        RETVAL=0
        pid=
        # Stop daemons.
        echo -n $"Stopping $prog: "
        pid=`/sbin/pidof -s $prog`
        if [ -n "$pid" ]; then
         kill -TERM $pid
        else
         failure $"$prog stop"
         echo
         return 1
        fi
        RETVAL=$?
        [ $RETVAL -ne 0 ] && failure $"$prog stop" 
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog && success $"$prog stop" 
        echo
        return $RETVAL
}
restart() {
        stop
# wait for forked daemons to die
        usleep 1000000
        start
}
  
# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        condrestart)
                [ -f /var/lock/subsys/$prog ] && restart
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|condrestart}"
                exit 1
esac
  
exit $?


 

6,给zabbix_agentd添加可执行的的权限并添加到到服务自启动。

[root@AppServerA zabbix]# chmod +x /etc/init.d/zabbix_agented 

[root@AppServerA zabbix]# chkconfig --add zabbix_agentd

[root@AppServerA init.d]# chkconfig zabbix_agented on


本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1693795