环境
安装iptraf-ng-1.2.1,可编译安装,安装包链接:iptraf-ng-1.2.1.zip
解压iptraf-ng-1.2.1
unzip ./iptraf-ng-1.2.1.zip |
安装ncurses(系统是ubuntu18.04)
sudo apt-get install libncurses5-dev |
默认是国外源,会报错,需要将软件源替换成国内源。软件源配置文件是 /etc/apt/sources.list
sudo vim /etc/apt/sources.list
将 http://mirrors.cn99.com/ubuntu/ 替换为: http://mirrors.aliyun.com/ubuntu/
然后执行:
sudo apt-get update |
如果还是会报错:Err:6 http://dl.google.com/linux/chrome/deb stable InRelease The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 78BD65473CB3BD13 Reading package lists... Done W: GPG error: http://dl.google.com/linux/chrome/
解决办法:
sudo vim /etc/apt/sources.list.d/google-chrome.list ### 打开 /etc/apt/sources.list.d/google-chrome.list 文件
修改文件内容:
原来是:deb http://dl.google.com/linux/chrome/deb/ stable main
改为: deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main
再次执行更新命令 sudo apt-get update |
如果上述操作不管用,可以进行如下操作:
sudo vim /etc/apt/sources.list.d/google-chrome.list
直接把这一行:deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main 注释掉
然后执行 sudo apt-get update
再进行
sudo apt-get install libncurses5-dev 就可以成功安装了 |
安装iptraf-ng-1.2.1,可编译安装
在编译安装之前需要安装的依赖包:
## 对于ubuntu18.04 需要换源,然后进行安装 对于ubuntu20.04 不需要换源,直接安装即可
sudo apt install libncursesw5
sudo apt install libtinfo-dev
sudo apt-get install libncurses5-dev
sudo apt install libncursesw5 libncursesw5-dev
sudo apt install build-essential
sudo apt install iptraf-ng
which iptraf-ng
cd iptraf-ng-1.2.1
sudo make
sudo make install
### ubuntu18.04 安装在/home/agora/sbin 下面
sudo mv /root/sbin/iptraf-ng /usr/sbin/iptraf-ng
### ubuntu20.04 安装在/root/sbin
sudo mv /root/sbin/iptraf-ng /usr/sbin/iptraf-ng |
配置logrotate:
执行:sudo vim /etc/logrotate.d/iptraf-ng
添加:
# Logrotate file for iptraf-ng
/var/log/iptraf-ng/*.log {
copytruncate
nocompress
dateext
dateformat .%Y%m%d%H%M%S
missingok
notifempty
rotate 20
create 0744 root root
}
sudo mkdir -p /var/log/iptraf-ng/. ## 建立log目录 |
后台运行iptraf-ng:
sudo /usr/sbin/iptraf-ng -B -i enp2s0 |
确认log是否正常输出:
ls -al /var/log/iptraf-ng |
确认log是否正常分割:
sudo logrotate -vf /etc/logrotate.d/iptraf-ng |
iperf-ng 守护脚本:
在/home/agora 目录下:
vim ./monitorP.sh
添加:
#!/bin/bash
pgrep "iptraf-ng"
if [ $? -ne 0 ]
then
/usr/sbin/iptraf-ng -B -i enp2s0 ## LAN口的网卡
fi
保存并退出 |
定时执行 log 分割 和守护脚本:
sudo crontab -e
添加:
# Rotate iptraf-ng log every 20mins
*/20 * * * * /usr/sbin/logrotate -vf /etc/logrotate.d/iptraf-ng
# Check iptraf-ng process every 5mins
*/5 * * * * /home/agora/monitorP.sh
保存并退出
执行命令: sudo crontab -l 确认是否设置成功 |
使用 脚本 iptraf_log_parser.py 进行数据解析和计算 。