分类: 系统运维
2016-05-26 16:14:37
在github上有一个库,用go语言写的,功能类似于系统的netstat 命令,可查看端口和服务
当然你也可以自己用别的语言去实现,恰巧最近在学go语言,所以就用它的了,贴一个简单的获取tcp端口服务的程序
TCP/UDP
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
|
package main
import (
"encoding/json"
"fmt"
"github.com/drael/GOnetstat"
"log"
)
type Process struct {
Name string `json:"{#PROCNAME}"`
Port int64 `json:"{#PORT}"`
}
type ZabbixData struct {
Data []Process `json:"data"`
}
func main() {
data := GOnetstat.Tcp()
var zd = new(ZabbixData)
for _, p := range data {
if p.State == "LISTEN" {
zd.Data = append(zd.Data, Process{p.Name, p.Port})
}
}
output, err := json.MarshalIndent(zd, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
}
|
编译为tcp_services,放到/usr/bin/目录下,并赋予执行权限
1
2
|
cp tcp_services /usr/bin/
chmod +x /usr/bin/tcp_services
|
测试
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
|
[root@proxy bin]# /usr/bin/tcp_services
{
"data": [
{
"{#PROCNAME}": "Master",
"{#PORT}": 25
},
{
"{#PROCNAME}": "Ruby",
"{#PORT}": 24224
},
{
"{#PROCNAME}": "Zabbix_agentd",
"{#PORT}": 10050
},
{
"{#PROCNAME}": "Zabbix_proxy",
"{#PORT}": 10051
},
{
"{#PROCNAME}": "Ruby",
"{#PORT}": 24230
},
{
"{#PROCNAME}": "Snmpd",
"{#PORT}": 199
},
{
"{#PROCNAME}": "Svnserve (Deleted)",
"{#PORT}": 3690
},
{
"{#PROCNAME}": "Nginx",
"{#PORT}": 80
},
{
"{#PROCNAME}": "Sshd",
"{#PORT}": 22
},
{
"{#PROCNAME}": "Ruby",
"{#PORT}": 8888
}
]
}
|
有以上返回,说明程序工作正常
我已编译好,大家可下载
编辑zabbix_agentd.conf
添加如下
1
|
UserParameter=udp_services, sudo /usr/bin/tcp_services
|
同时编辑,/etc/sudoers.d/zabbix
1
2
3
|
vi /etc/sudoers.d/zabbix
Defaults:zabbix !requiretty
zabbix ALL=NOPASSWD: /usr/bin/tcp_services
|
我就添加一个tcp的,udp的可照着添加一个就可以了,添加好之后重启zabbix_agentd
zabbix里导入模版,导入之后,添加模版到主机
A.检查是否关闭SElinux
B.在zabbix_server上执行操作(IP_DO_SERVIDOR_MONITORADO为被监控端ip
1
|
zabbix_get -s IP_DO_SERVIDOR_MONITORADO -k "tcp_services"
|
例如
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
|
[root@canghai bin]# ./zabbix_get -s 192.168.1.10 -k "tcp_services"
{
"data": [
{
"{#PROCNAME}": "Master",
"{#PORT}": 25
},
{
"{#PROCNAME}": "Ruby",
"{#PORT}": 24224
},
{
"{#PROCNAME}": "Zabbix_agentd",
"{#PORT}": 10050
},
{
"{#PROCNAME}": "Zabbix_proxy",
"{#PORT}": 10051
},
{
"{#PROCNAME}": "Ruby",
"{#PORT}": 24230
},
{
"{#PROCNAME}": "Snmpd",
"{#PORT}": 199
},
{
"{#PROCNAME}": "Svnserve (Deleted)",
"{#PORT}": 3690
},
{
"{#PROCNAME}": "Nginx",
"{#PORT}": 80
},
{
"{#PROCNAME}": "Sshd",
"{#PORT}": 22
},
{
"{#PROCNAME}": "Ruby",
"{#PORT}": 8888
}
]
}
|
如返回数据说明没有问题,不返回则说明zabbix_agentd或者tcp_service有问题。
模版&tcp_services(Linux_x32&x64)下载:
此文为参考老外文章,经测试udp_serivces服务提取为空。老外原文地址: