我这边有个比较大的B/S系统,现在需要对各用户使用该系统时候的实时流量进行监控,
需要采集的实时信息有用户的ip地址、流量,然后在web页面进行实时展示。前期想到是用zabbix进行监控,但这个需求是和业务挂钩的,不想在服务器安装zabbix,所以就没有采用该方法了。各位有什么好的方法推荐没?
iftop 命令可以查看到吧
iftop iftop iftop 命令可以查看到吧
站长工具引入即可,别搞得那么费劲
java的话,可以自己在apache,ngnix等web服务器上挂钩来统计流量,这样最精确,而且可以分用户统计。然而会有一些cpu开销。
用python、java等语言调用
iftop -B -t -s 1,读取 输出,并提取stdout
import os
output = os.popen('iftop -B -t -s 1')
print output
Java
public class test {
public static void main(String[] args){
InputStream in = null;
try {
Process pro = Runtime.getRuntime().exec(new String[]{"iftop",
"-t","-B",
"-s","1"});
pro.waitFor();
in = pro.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String result = read.readLine();
System.out.println("INFO:"+result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
数据示例
interface: eth0
IP address is: 10.104.162.250
MAC address is: 52:54:00:34:42:0b
Listening on eth0
# Host name (port/service if enabled) last 2s last 10s last 40s cumulative
--------------------------------------------------------------------------------------------
1 10.104.162.250 => 146B 146B 146B 292B
183.240.27.134 <= 208B 208B 208B 416B
2 10.104.162.250 => 48B 48B 48B 96B
10.59.171.76 <= 48B 48B 48B 96B
--------------------------------------------------------------------------------------------
Total send rate: 194B 194B 194B
Total receive rate: 256B 256B 256B
Total send and receive rate: 450B 450B 450B
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total): 194B 256B 450B
Cumulative (sent/received/total): 388B 512B 900B
============================================================================================
读到这个数据了,怎么处理就看你了。挺容易了。
如果想要数据的某一部分 用sed和 awk进一步处理一下就好了