Skip to content

Commit

Permalink
Version 5.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chn-lee-yumi committed Aug 14, 2018
1 parent 4fc0372 commit fee0b07
Show file tree
Hide file tree
Showing 42 changed files with 1,102 additions and 853 deletions.
25 changes: 22 additions & 3 deletions latest/Config.py → Config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# encoding: utf-8
# =============网页参数=============
WEB_USERNAME = 'admin' # 网页登录的用户名
WEB_PASSWORD = 'admin' # 网页登录的密码
WEB_PASSWORD = '12345' # 网页登录的密码
WEB_PORT = 80 # http端口

# =============Ping模块参数=============
TCPING_TIMEOUT = 2 # tcping超时时间,秒

# =============监控参数=============
# DEAFULT_PING_INTERVAL = 30 # 扫描间隔,秒,默认30
# DEAFULT_CPU_INTERVAL = 1 # CPU扫描间隔,分钟,默认1
Expand All @@ -12,9 +15,25 @@
# SWITCH_OVERLOAD_ALERT_TIMES = 3 # 接入交换机连续多少轮扫描都overload才报警,默认3
# SWITCH_UP_AND_DOWN_ALERT_TIMES = 5 # 接入交换机连续挂了又复活多少次才报警,默认5
# DELAY_STAT_TIME = 30 # 延迟发统计信息的时间,秒,默认30
TCPING_TIMEOUT = 2 # tcping超时时间,秒
HELPDESK_TIME = 5 # 微信通知/发送工单的延迟,分钟
WEIXIN_STAT_TIME_H = 18 # 发送微信统计的时
WEIXIN_STAT_TIME_M = 0 # 发送微信统计的分
SW_REBOOT_TIME_H = 4 # 自动重启交换机的时
SW_REBOOT_TIME_M = 0 # 自动重启交换机的分
SW_REBOOT_TIME_M = 0 # 自动重启交换机的分
CPU_THRESHOLD = 80 # CPU过载阈值
MEM_THRESHOLD = 80 # 内存过高阈值
TEMP_THRESHOLD = 60 # 温度过高阈值
DATA_RECORD_INTERVAL = 5 # 历史记录保存间隔,单位分钟
DATA_RECORD_SAVED_DAYS = 2 # 历史记录保留天数

# =============SNMP参数=============
SNMP_READ_COMMUNITY = "123456"
SNMP_WRITE_COMMUNITY = "654321"

# =============交换机密码=============
SWITCH_PASSWORD = "3.141592653589793238"

# =============微信接入参数=============
corpid = "wxsakgheity3w9t"
corpsecret = ['', '', 'DFery43g45fW45tgg4th64hdsghiwhil, '', '', '',
'URD45VB7jkg89gu8YNIUG78i87b89n8B8n890bUg']
129 changes: 91 additions & 38 deletions latest/Controller.py → Controller.py

Large diffs are not rendered by default.

674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

60 changes: 54 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
# switch-monitor
广东工业大学交换机监控,2016年“数字校园”学生科技项目。(已结题,但仍在持续更新)
由于权限问题,目前只监控大学城校区学生宿舍的交换机和生活区核心交换机。未对教学区和其它校区的交换机进行适配。
最新版本为5.0.0,在latest文件夹内。
最新版本及历史版本的压缩包在release文件夹内。
安装部署配置详细说明见latest文件夹下的readme.txt。
# 广东工业大学交换机监控
- 2016年“数字校园”学生科技项目。(已结题,但仍在持续更新)
- 由于权限问题,目前只监控大学城校区学生宿舍的交换机和生活区核心交换机。未对教学区和其它校区的交换机进行适配。
- 最新版本为5.1.0。
- 使用GPLv3开源协议。

# 部署说明
## 设备
- 推荐配置:
- CPU:4核
- 内存:1GB
- 可运行在树莓派2B上。
## 系统环境
- Windows和Linux均可
- 环境:Python3
- 依赖包:requests、flask
## Debian系统部署
```shell
apt-get install python3 snmp
pip3 install requests
pip3 install flask
python3 SwitchMonitor.py # 运行
```
## Windows系统部署
- 从python.org下载安装python3
- 在cmd输入下面命令
```shell
pip install requests
pip install flask
python SwitchMonitor.py # 运行
```
## 参数说明
- 必须要修改的参数:(Config.py)
- web_username:网页用户名
- web_password:网页登录密码
- switch_password:交换机密码
- corpid:微信开放平台corpid
- corpsecret:微信开放平台corpsecret
- SNMP_COMMUNITY:SNMP读写密码
- 默认报警参数(详见Config.py)
- 交换机掉线5分钟后发送微信通知以及提交工单。
- 每天下午18点发送统计信息。每天凌晨4点自动重启过载的交换机。
- CPU过载阈值:80%
- 内存过高阈值:80%
- 温度过高阈值:60℃

# 更新日志
- v5.1.0:
- 整合部分参数到配置文件
- 性能调优,加快启动速度,减少CPU占用。
- 现在交换机端口历史流量页面修改单位后不会重新获取数据,提高了相应速度。
- 修复一些小BUG
- v5.0.0:
- 重构代码。
File renamed without changes.
6 changes: 4 additions & 2 deletions latest/SwitchMonitor.py → SwitchMonitor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/python
# encoding: utf-8
#!/usr/bin/python3
# encoding: utf-8

'''
说明:该脚本用于启动交换机监控。
'''

from Controller import start_switch_monitor

start_switch_monitor()
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 0 additions & 33 deletions latest/readme.txt

This file was deleted.

15 changes: 13 additions & 2 deletions latest/mod_helpdesk.py → mod_helpdesk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8
import json

import requests


Expand Down Expand Up @@ -107,7 +106,7 @@ def send_incident(building_name, switch_ip):

# 模拟登录。用户名密码是加密过的,在浏览器用F12抓包获得。
a = requests.get(
"http://helpdesk1.gdut.edu.cn/portal/edulogin?username=12345%3D%3D&password=12345&safe=1")
"http://helpdesk1.gdut.edu.cn/portal/edulogin?username=MzExNTAwNjI1NA%3D%3D&password=nqrstu&safe=1")
print(a.cookies)
# print(a.text)

Expand Down Expand Up @@ -154,6 +153,8 @@ def send_incident(building_name, switch_ip):
print(e_orgid)
print(e_orgname)
# http://helpdesk.gdut.edu.cn/portal/wfe/getUsers.action?activityId=d3d147a82cf0458381ca79ff39c8ff08&isBack=N&appId=INC-10100282&hasextenduser=Y&formId=1
# print(json.loads(e.text)[0]["orgname"])
# 返回的内容[{"activityid":"","orgid":"3115006254","orgname":"利昱旻","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"3116002852","orgname":"何永宝","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"3116001555","orgname":"梁鸿楷","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"ff80808142c70b5b0142c84ed7d1002d","orgname":"张诗健","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"ff808081484d8ffc0149079fc5af769c","orgname":"曾任","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0}]

# 工单加绿色点
f = requests.post("http://helpdesk.gdut.edu.cn/portal/sla/sla_addRecord.action?id=" + c["appId"],
Expand Down Expand Up @@ -189,3 +190,13 @@ def send_incident(building_name, switch_ip):
if __name__ == '__main__':
send_incident("东二", "172.16.102.1")

'''
<RequestsCookieJar[<Cookie CASTGC=TGC-453-oq4t649V9XOpW7vCVk7GduicPdqVKJezl1luk30IctzuIomW5v for .helpdesk.gdut.edu.cn/portal>, <Cookie JSESSIONID=102FB5780DFED3FDF6B9B8EC16CC1D57 for helpdesk.gdut.edu.cn/portal>]>
{"activityId":"84d8011ea67d4502b461e8637e02ff25","activityInstanceId":"5e48e28b5f52116b015f682d8784179a","activityName":"开始","appId":"INC-10179970","author":"3115006254","authorName":"利昱旻","docStatus":"","formId":"1","itemId":"5e48e28b5f52116b015f682d8784179b","parentProcessInstanceId":"","processId":"ebfbdae025bb44c093ab7ad8d3520830","processInstanceId":"5e48e28b5f52116b015f682d87841799","processName":"","status":"new","title":"交换机故障检查 172.16.102.1"}
INC-10179970
[{"activityid":"","orgid":"3115006254","orgname":"利昱旻","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"3116002852","orgname":"何永宝","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"3116001555","orgname":"梁鸿楷","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"3117003167","orgname":"许锦涛","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0},{"activityid":"","orgid":"3117001776","orgname":"范家演","orgtype":"YG","parentnames":"","participantid":"","processid":"","showorder":0}]
3115006254,3116002852,3116001555,3117003167,3117001776,
利昱旻,何永宝,梁鸿楷,许锦涛,范家演,
工单提交状况:{"flg":"1","msg":"提交成功!"}
微信推送状况:{"scuess":1}
'''
File renamed without changes.
3 changes: 1 addition & 2 deletions latest/mod_ping.py → mod_ping.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import socket

from Config import *
from Config import TCPING_TIMEOUT

socket.setdefaulttimeout(TCPING_TIMEOUT)

Expand Down
6 changes: 2 additions & 4 deletions latest/mod_reboot_switch.py → mod_reboot_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import traceback
import mod_snmp

from Config import SWITCH_PASSWORD

'''
该模块用于重启交换机
Expand All @@ -12,8 +12,6 @@
无返回值。
'''

switch_password = "123456" # 密码


def reboot_switch_telnet(ip):
try:
Expand All @@ -25,7 +23,7 @@ def reboot_switch_telnet(ip):
# 输入登录密码
print('Connected. Logining...')
tn.read_until('assword:'.encode('gbk'), 5)
tn.write(switch_password.encode('gbk') + b'\n')
tn.write(SWITCH_PASSWORD.encode('gbk') + b'\n')
# 登录完毕后执行命令
tn.read_until('>'.encode('gbk'), 5)
print('Login succeed! Rebooting...')
Expand Down
35 changes: 20 additions & 15 deletions latest/mod_snmp.py → mod_snmp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# encoding: utf-8
import platform
import subprocess
from Config import SNMP_READ_COMMUNITY, SNMP_WRITE_COMMUNITY

# TODO: SNMP模块现在在windows下会有奇怪的bug,popen的时候只要加了-O qv就没有回显,但在cmd中执行命令是有回显的。

'''
该模块用于使用SNMP获取交换机信息,调用了snmpwalk.exe,若是linux系统,修改一下SNMP_BIN_PATH即可。
Expand Down Expand Up @@ -36,9 +39,7 @@
SNMP_TIMEOUT = "1"
SNMP_RETRY_TIMES = "3"
SNMP_VER = "2c"
SNMP_READ_COMMUNITY = "read"
SNMP_WRITE_COMMUNITY = "write"
if platform.system() == "Windows": # windows系统不知道出什么问题了,snmp用不了
if platform.system() == "Windows":
SNMP_WALK_BIN_PATH = ".\\bin\\snmpwalk"
SNMP_SET_BIN_PATH = ".\\bin\\snmpset"
else:
Expand Down Expand Up @@ -124,7 +125,7 @@ def SnmpWalk(ip, model, info):
a = subprocess.Popen(
[SNMP_WALK_BIN_PATH, "-O", "qv", "-t", "1", "-r", "3", "-v", SNMP_VER, "-c", SNMP_READ_COMMUNITY, ip, oid],
bufsize=0, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
b = a.stdout.read().decode('utf-8')[:-1]
b = a.stdout.read().decode('utf-8').strip('\n')
if b == "": return "获取失败"
if b.find("No Such Object") >= 0: return "设备不支持"
if return_list == True:
Expand Down Expand Up @@ -155,11 +156,11 @@ def SnmpSet(ip, model, info):
try:
a = subprocess.Popen(
[SNMP_SET_BIN_PATH, "-O", "qv", "-t", "1", "-r", "3", "-v", SNMP_VER, "-c", SNMP_WRITE_COMMUNITY, ip, oid,
type,
value], bufsize=0, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
type, value], bufsize=0, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# snmpwalk -v 2c -c gdgydx_pub 172.16.111.1 1.3.6.1.2.1.2.2.1.2
# snmpwalk -v 2c -c gdgydx_pub 172.16.111.1 1.3.6.1.2.1.2.2.1.10
b = a.stdout.read().decode('utf-8')[:-1]
b = a.stdout.read().decode('utf-8').strip('\n')
# 下面清空流,防止爆内存,参考http://blog.csdn.net/pugongying1988/article/details/54616797
if a.stdin:
a.stdin.close()
Expand Down Expand Up @@ -191,7 +192,6 @@ def SnmpSet(ip, model, info):
[SNMP_WALK_BIN_PATH, "-O", "qv", "-t", SNMP_TIMEOUT, "-r", SNMP_RETRY_TIMES, "-v", SNMP_VER, "-c",
SNMP_COMMUNITY, ip, oid], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return b.stdout.read().decode('utf-8')[:-1] # 去掉回车和换行符
'''

Expand All @@ -202,21 +202,26 @@ def get_oid(model, info):
'''

if __name__ == '__main__': # SNMP测试
# print(SnmpWalk("172.16.101.1", "S2700", "up_time"))
ip = "172.16.254.1"
oid = "1.3.6.1.2.1.2.2.1.9"
a = subprocess.Popen(
[SNMP_WALK_BIN_PATH, "-O", "qv", "-t", "1", "-r", "3", "-v", SNMP_VER, "-c", SNMP_READ_COMMUNITY, ip, oid],
bufsize=0, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# a = subprocess.Popen(
# [SNMP_WALK_BIN_PATH, "-O", "qv", "-t", "5", "-r", "2", "-v", SNMP_VER, "-c", "index", "demo.snmplabs.com",
# "1.3.6.1.4.1.20408.999.1.1.1"], bufsize=0, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
# stderr=subprocess.PIPE)
a = subprocess.Popen(
[SNMP_WALK_BIN_PATH, "-t", SNMP_TIMEOUT, "-r", SNMP_RETRY_TIMES, "-v", SNMP_VER, "-c",
SNMP_READ_COMMUNITY, "172.16.254.1", "1.3.6.1.2.1.2.2.1.9"], bufsize=0, shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
'''a = subprocess.Popen(
[SNMP_WALK_BIN_PATH, "-t", SNMP_TIMEOUT, "-r", SNMP_RETRY_TIMES, "-v", SNMP_VER, "-c",
SNMP_READ_COMMUNITY, "172.16.254.1", "1.3.6.1.2.1.2.2.1.9"], bufsize=0, shell=False,stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)'''
# a = subprocess.Popen(
# [SNMP_SET_BIN_PATH, "-O", "qv", "-t", "1", "-r", "3", "-v", SNMP_VER, "-c", SNMP_WRITE_COMMUNITY, "172.16.102.1", "1.3.6.1.4.1.2011.5.25.19.1.3.2.0", "i",
# "3"], bufsize=0, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
a = a.stdout.read().decode()
# a = a.stdout.read().decode()

print(a)
print(a.stdout.read().decode())
print("*" * 50)

'''
Expand Down
7 changes: 2 additions & 5 deletions latest/mod_weixin.py → mod_weixin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# encoding: utf-8
import json
import time

import requests
from Config import corpid,corpsecret

'''
该模块用于发送微信推送
Expand All @@ -16,10 +16,7 @@
TODO:处理网络异常。
'''

corpid = "wx01d7asdga2345c36"
token = ['', '', '', '', '', '', '']
corpsecret = ['', '', 'XbVPLqxasadsfdfdshadfgsd _ASDfasdfbREnCvZQ5IgwAOS8', '', '', '',
'tXrTsdpzWpxRO-V1QA--asdjfa324FDEHV6p3YTsdfaXiWjasdgUzZOs1JpM']
token = [''] * len(corpsecret)


def get_token(agentid):
Expand Down
File renamed without changes.
Binary file removed release/source-1.x.x.zip
Binary file not shown.
Binary file removed release/source-2.x.x.zip
Binary file not shown.
Binary file removed release/source-3.2.12.zip
Binary file not shown.
Binary file removed release/source-3.5.28.zip
Binary file not shown.
Binary file removed release/source-4.0.0.zip
Binary file not shown.
Binary file removed release/source-4.1.0.zip
Binary file not shown.
Binary file removed release/source-4.1.1.zip
Binary file not shown.
Binary file removed release/source-4.3.2.zip
Binary file not shown.
Binary file removed release/switch-monitor-4.3.3.zip
Binary file not shown.
Binary file removed release/switch-monitor-5.0.0.zip
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit fee0b07

Please sign in to comment.