CentOS7下collectd简单安装使用

本文详细介绍了在CentOS7上安装和配置collectd的过程,包括设置配置文件,将数据写入日志和RRD文件,前端数据展示,设置server client端以及将数据传输到InfluxDB。此外,还讲解了如何使用virt插件记录KVM虚拟机数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CentOS7安装collected,基本设置。

yum -y install epel*
yum -y install collectd

开启服务并设置开机启动

systemctl start collectd
systemctl enable collectd

尚未对collectd的配置文件进行任何设置。默认日志是系统日志,写入/var/log/messages

tail -f /var/log/messages 
设置配置文件。

默认的配置文件/etc/collectd.conf

收集的数据通过不同的收集插件定义,默认collectd收集的数据有

LoadPlugin cpu
LoadPlugin interface
LoadPlugin load
LoadPlugin memory
无注释表示启用了该插件。
#表示该插件开发完毕,可以使用(需要下载相应的插件包,然后取消注释)
##表示该插件还未完成,无法使用。
默认收集间隔是 10s一次
每个插件都有单独的配置,不设置就使用插件的默认配置。
设置将日志写入单独的日志文件

编辑配置文件

 vim /etc/collectd.conf 
#LoadPlugin syslog
LoadPlugin logfile

<Plugin logfile>
        LogLevel info
        File "/var/log/collectd.log"
        Timestamp true
        PrintSeverity false
</Plugin>

重启collectd

systemctl restart collectd

查看日志

tail -f /var/log/collectd.log 
[root@client2 ~]# tail -f /var/log/collectd.log 
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:24 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Filter subsystem: Built-in target `write': Dispatching value to all write plugins failed with status 2 (ENOENT). Most likely this means you didn't load any write plugins.
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]
Jan  4 14:24:34 client2 collectd[10794]: Available write targets: [none]

collectd必须要设置一个write pulgin 写插件,可以将收集的数据写入日志、rdd文件、server端、influxdb数据库。总之必须设置一个,不然collectd无用。

设置将数据写入日志文件。

一般是不会写入日志文件当作write pulgin的。只是用来测试数据收集是否成功。
编辑配置文件。

vim /etc/collectd.conf 
LoadPlugin write_log

重启collectd

systemctl restart collectd

查看日志

[root@server ~]# tail -f /var/log/collectd.log 

[2021-01-04 14:39:37] write_log values:
server.memory.memory-free 248778752 1609742377

[2021-01-04 14:39:37] write_log values:
server.memory.memory-slab_unrecl 41025536 1609742377

[2021-01-04 14:39:37] write_log values:
server.memory.memory-slab_recl 39510016 1609742377

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-user 2452 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-system 2868 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-wait 2547 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-nice 153 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-interrupt 0 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-softirq 77 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-steal 0 1609742387

[2021-01-04 14:39:47] write_log values:
server.cpu-0.cpu-idle 241882 1609742387

[2021-01-04 14:39:47] write_log values:
server.interface-lo.if_packets.rx 0 1609742387
server.interface-lo.if_packets.tx 0 1609742387

可以看到数据成功写入日志文件了。

设置将数据写入rrd文件。

collectd将数据以rrd文件形式保存,需要相应的rrdtool包。除了基本的几个,其余的collectd插件基本都需要单独安装插件包才能使用。

yum -y install collectd-rrdtool
vim /etc/collectd.conf 

插件rrdtool设置使用默认的即可

LoadPlugin rrdtool

#<Plugin rrdtool>
#       DataDir "/var/lib/collectd/rrd"
#       CreateFilesAsync false
#       CacheTimeout 120
#       CacheFlush   900
#       WritesPerSecond 50
#</Plugin>
systemctl restart collectd

rrd数据保存位置/var/lib/collectd/rrd
server当前主机同名目录中保存的就是 当前主机的收集内容
collectd收集数据写到rrd文件

前端数据展示。

CentOS7下提供了一个简单前端查看collectd收集数据的包collectd-web
通过collection3 + http前端展示数据。

[root@server ~]# yum info collectd-web
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.huaweicloud.com
Available Packages
Name        : collectd-web
Arch        : x86_64
Version     : 5.8.1
Release     : 1.el7
Size        : 47 k
Repo        : epel/x86_64
Summary     : Contrib web interface to viewing rrd files
URL         : https://collectd.org/
License     : GPLv2
Description : This package will allow for a simple web interface to view rrd files created by
            : collectd.

https://fishilico.github.io/generic-config/etc-server/web/collectd.html

1.安装collectd-web

yum -y install collectd-web

安装完成 /usr/share/collectd/下多出collection3目录。

2.编辑文件设置rrd数据目录

vim /usr/share/collectd/collection3/etc/collection.conf 
DataDir "/var/lib/collectd/rrd"

3.设置httpd使用collection3

vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    Alias /collection3/ /usr/share/collectd/collection3/
    ScriptAlias /collection3/bin/ /usr/share/collectd/collection3/
    <Directory /usr/share/doc/collectd-core/examples/collection3/>
        AddHandler cgi-script .cgi
        DirectoryIndex bin/index.cgi
        Options +ExecCGI
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

注 后面的路径就是。安装collectd-web后多出来的。

Alias /collection3/ /usr/share/collectd/collection3/
ScriptAlias /collection3/bin/ /usr/share/collectd/collection3/

4.设置外部访问
设置http下collectd的访问配置。

vim /etc/httpd/conf.d/collectd.conf 

将local注释掉,改为all granted

<Directory "/usr/share/collectd/collection3/">
    #Require local
    Require all granted
    DirectoryIndex bin/index.cgi
    DirectoryIndexRedirect on
</Directory>

<Directory "/usr/share/collectd/collection3/etc/">
    Require all denied
</Directory>

<Directory "/usr/share/collectd/collection3/lib/">
    Require all denied
</Directory>

<Directory "/usr/share/collectd/collection3/share/">
    # Require local
    Require all granted
</Directory>

<Directory "/usr/share/collectd/collection3/bin/">
    Options ExecCGI
    AddHandler cgi-script .cgi
    # Require local
    Require all granted
</Directory>

5.启动httpd

systemctl enable httpd
systemctl restart httpd

访问http:IP/collection3/
collection3/ 是3中设置的 Alias /collection3/ /usr/share/collectd/collection3/
collection3前端

设置server client端。

collectd可以通过network插件设置server/client端。将client端的数据通过network发送到server端的指定端口,server端收集端口数据保存。
一个client端可以发送数据到不同的server端,一个server端也可以收集不同端口的数据。

client 端设置

vim /etc/collectd.conf 
LoadPlugin network

<Plugin network>
        Server "192.168.199.80" "25826"
</Plugin>

将数据发送到192.168.199.80主机的25826端口。

server 端设置。数据保存成rrd

vim /etc/collectd.conf 
LoadPlugin rrdtool
LoadPlugin network

<Plugin network>
        Listen "192.168.199.80" "25826"
</Plugin>

监听本机192.168.199.80的25826端口

重启collectd

systemctl restart collectd

server查看保存的rrd数据。

ls -l /var/lib/collectd/rrd/

network插件

三个主机的前端

将数据传输到influxdb数据库。

influxdb支持collectd数据,设置influxdb的collectd段配置收集端口数据即可(不可与collectd listen端口相同。所以一般设置influxdb就不再设置collectd server,测试可以设置client发送数据到多个端口)。可以将influxdb 看成一个collectd server。

修改influxdb设置

vim /etc/influxdb/influxdb.conf
[[collectd]]
    enabled = true
    bind-address = ":25827"

只设置enabled为true 和端口号。。。其他的默认
重启influxdb

systemctl restart influxdb

查看是否监听端口

netstat -nlp | grep 25827
[root@server ~]# netstat -nlp | grep 25827
udp6       0      0 :::25827                :::*                                11924/influxd       
[root@server ~]# netstat -nlp | grep 25826
udp        0      0 192.168.199.80:25826    0.0.0.0:*                           11851/collectd    

设置collectd的client端发送数据到25827端口

<Plugin network>
        Server "192.168.199.80" "25826"
        Server "192.168.199.80" "25827"
</Plugin>
systemctl restart collectd

注:一个network下可以既有Server又有Listen,即服务端也可以发送自身的数据到其他server。

查看influxdb中的collectd数据。
influxdb下的collectd数据collectd

设置virt插件记录kvm虚拟机数据。

collectd可以借助libvirtd的API收集虚拟机的数据。只需安装virt插件。

yum -y install collectd-virt
vim /etc/collectd.conf 

qemu管理的虚拟机

LoadPlugin virt

<Plugin virt>
        Connection "qemu:///system"
        HostnameFormat name
</Plugin>
systemctl restart collectd

需要libvirtd服务启动。
记录的虚拟机信息保存在 /var/lib/collectd/虚拟机同名目录下。
当虚拟机被删除时,collectd不会删除该目录,只是会停止记录该虚拟机的数据。
原信息如需删除需手动删除目录。如果使用collection3简单前端展示虚拟机信息,将目录删除后 该虚拟机的信息展示自动消失。

virt的配置 一般只用设置上面的即可
https://github.com/collectd/collectd/blob/master/docs/README.virt.md

#       Domain "name"			#设置该行 则virt只会监控domain为"name"的虚拟机
#       BlockDevice "name:device"	#同上设置后只会监控设置的块
#       BlockDeviceFormat target		# 磁盘块的表示格式
#       BlockDeviceFormatBasename false	# 不太理解
#       InterfaceDevice "name:device"	# 只会监控设置的 虚拟网卡
#       IgnoreSelected false			# 不理解
#       HostnameFormat name				# 虚拟机的domain展示格式可以是name uuid 等。id基本相当于name 
#       InterfaceFormat name				# 虚拟网卡的展示格式 可以是name、 address(IPv6)
#       PluginInstanceFormat name		# 不理解

Format可以设置的值不清楚,设置监控固定的DEVICE或domain也没必要。。。
一般只设置 即可

<Plugin virt>
        Connection "qemu:///system"
        HostnameFormat name
</Plugin>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值