程序猿全敏的gravatar头像
程序猿全敏 2016-11-07 11:45:15

java和python结合项目,实现ajax+echarts显示端口占用数监控

如何运行请参考博客:关于“java和python结合项目,实现ajax+echarts显示端口占用数监控”怎么运行

大家好!又到了一周的上班时间,今天给大家分享一个之前做的端口占有数的监控项目

项目经理提出要监控好几个服务器的端口的占有数,就要我想办法,其实这个开始觉得这个好像很难的样子,后来想通了就很简单了。就是那个echarts这个我也是听伊成,饕餮他们说的,然后我就打开了echart的官网,他们要我自学echarts,我才不去学。直接在官网复制一个demo,静态数据,看起来还是很ok的。

然后整理思路:

首先是用python执行cmd命令,输出端口数的文本放到本地,java就调用文本里面的内容显示在界面上。思路都清楚了,就开始代码实现了。

python代码执行cmd命令,输出端口数的代码

import re
import os
from threading import Timer
import time
def execCmd(cmd):
	r = os.popen(cmd)
	text = r.read()
	r.close()
	return text
def sleeptime(hour,min,sec):
	return hour*3600 + min*60 + sec;
second = sleeptime(0,0,60);
while 1==1:
	time.sleep(second);
	#if __name__ == '__main__':
	cmd = 'netstat -an |find /c ":80"'
	cmd1 = 'netstat -an |find /c ":8080"'
	cmd2 = 'netstat -an |find /c ":8888"'
	cmd3 = 'netstat -an |find /c ":8080"'
	result = execCmd(cmd)
	result1 = execCmd(cmd1)
	result2 = execCmd(cmd2)
	result3 = execCmd(cmd3)
	print("80端口数为:"+result)
	print("8080端口数为:"+result1)
	print("8888端口数为:"+result2)
	print("8080端口数为:"+result3)
	x = open("test.txt", 'a')
	f =open("test2.txt",'a')
	y=open("test3.txt",'a')
	z=open("test4.txt",'a')
	x.write(result)
	f.write(result1)
	y.write(result2)
	z.write(result3)
	x.close()
	f.close()
	y.close()
	z.close()

	

cmd命名每1分钟执行一次,得到的结果如图

java和python结合项目,实现ajax+echarts显示端口占用数监控

同时同路径下得到几个文本。输出这么多的端口是为了便于查看,看起来就是高端大气的感觉

然后就开始用java代码调用文本

public static Object[] readTxtFile(String filePath) {
		InputStreamReader read = null;
		BufferedReader bufferedReader = null;
		Object[] result = null;
		try {
			String encoding = "GBK";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				ArrayList<String> list = new ArrayList<String> ();
				while ((lineTxt = bufferedReader.readLine()) != null) {
			        list.add (lineTxt);
				}
				result = list.toArray ();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) 
					bufferedReader.close();
				if (read != null) 
					read.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

根据echarts demo 把得到的数据封装成json格式

String[] categories = {"192.168.15.130的80端口"};  
		String[] categories2 = {"192.168.15.130的8080端口"};
		String[] categories3 = {"192.168.15.131的80端口"};
		String[] categories4 = {"192.168.15.131的8080端口"};
	    Object[] values = Port.readTxtFile(filePath);
	    Object[] values2 = Port.readTxtFile(filePath2);
	    Object[] values3 = Port.readTxtFile(filePath3);
	    Object[] values4 = Port.readTxtFile(filePath4);
	    Map<String, Object> json = new HashMap<String, Object>(); 
	    json.put("categories", categories);
	    json.put("categories2", categories2); 
	    json.put("categories3", categories3); 
	    json.put("categories4", categories4); 
	    json.put("values", values);
	    json.put("values2", values2);
	    json.put("values3", values3);
	    json.put("values4", values4);
	    PrintWriter out = response.getWriter();
	    out.write(JsonUtil.toJson(json));

然后前端取得数据

java和python结合项目,实现ajax+echarts显示端口占用数监控

希望对大家有所帮助,我做的都是些监控之类的东西。

不懂的地方可以加我qq 1610656207 备注最代码


最代码官方编辑于2016-11-7 21:35:14

打赏

文件名:dk.rar,文件大小:1375.598K 下载
  • /
      • /dk
        • /dk/.classpath
        • /dk/.project
          • /dk/.settings
            • /dk/.settings/.jsdtscope
            • /dk/.settings/org.eclipse.jdt.core.prefs
            • /dk/.settings/org.eclipse.wst.common.component
            • /dk/.settings/org.eclipse.wst.common.project.facet.core.xml
            • /dk/.settings/org.eclipse.wst.jsdt.ui.superType.container
            • /dk/.settings/org.eclipse.wst.jsdt.ui.superType.name
最代码最近下载分享源代码列表最近下载
75431368  LV10 2021年7月16日
马儿爱吃兰  LV10 2021年6月15日
wangshixi2010  LV3 2020年8月21日
wjschm  LV14 2020年4月27日
huaua7676  LV30 2020年1月15日
benyan  LV8 2019年4月29日
阿毛123456  LV13 2019年4月14日
dmzhjg  LV6 2018年8月31日
shiwenliang  LV5 2018年5月30日
byj1987  LV18 2018年5月13日
最代码最近浏览分享源代码列表最近浏览
666ing  LV2 2023年9月27日
flyjoe  LV3 2023年7月11日
ericxu1116  LV24 2023年6月26日
2292250314  LV2 2023年5月28日
王培龚  LV4 2023年4月10日
hkxyyz  LV6 2022年11月14日
lilu0226  LV7 2022年7月31日
帅潇潇  LV12 2022年6月13日
Trickster  LV9 2022年5月17日
crosa_Don  LV18 2022年4月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友