Skip to content

Commit 7a33e3d

Browse files
committed
rebuild
1 parent 3e7164c commit 7a33e3d

9 files changed

Lines changed: 611 additions & 162 deletions

File tree

chatops/settings.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@
104104
},
105105
]
106106

107+
CACHES = {
108+
'default': {
109+
'BACKEND': 'django_redis.cache.RedisCache',
110+
'LOCATION': 'redis://47.103.14.52:6380',
111+
"OPTIONS": {
112+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
113+
"PASSWORD": "qwe@123..asd",
114+
},
115+
},
116+
}
117+
107118

108119
# Internationalization
109120
# https://docs.djangoproject.com/en/3.0/topics/i18n/
@@ -117,7 +128,7 @@
117128
USE_L10N = True
118129

119130
USE_TZ = True
120-
ZABBIX_URL="http://47.103.14.52:8088/api_jsonrpc.php"
131+
ZABBIX_URL="http://47.103.14.52:8080/api_jsonrpc.php"
121132
ZABBIX_USER="Admin"
122133
ZABBIX_PASSWORD="zabbix"
123134
# Static files (CSS, JavaScript, Images)

chatops/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
from dingding import views
1919
urlpatterns = [
2020
path('admin/', admin.site.urls),
21-
path('dingding',views.robot,name='robot')
21+
path('dingding',views.robot,name='robot'),
22+
path('web',views.robot,name='web'),
2223
]

dingding/data.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,ip,cipan,neicun,cpu,clock
2+
docker,60.205.177.168,93.9064,32.8043,0.0500,2020-05-27 00:02:12

dingding/jenkins.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
#Author: wanger
4+
#Email: cxf210210@163.com
5+
#File:jenkins.py
6+
#CreateTime:2020/6/15 16:21
7+
# __Software__: PyCharm
8+
class jenkins:
9+
def __init__(self):
10+
pass

dingding/ping.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# -*- coding:utf-8 -*-
33
# __author__ = 'bigc'
44
# @Time : 2020/4/12 21:36
5-
# @Email : luocs1@lenovo.com
65
# views部分代码
76

87
from django.http import HttpResponse, JsonResponse

dingding/shell.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
#Author: wanger
4+
#Email: cxf210210@163.com
5+
#File:shell.py
6+
#CreateTime:2020/6/15 16:10
7+
# __Software__: PyCharm
8+
from django.http import HttpResponse, JsonResponse
9+
import json, re,paramiko
10+
from django.core.cache import cache
11+
12+
class shell:
13+
def __init__(self,content):
14+
self.user='root'
15+
self.password='qwe@123..asd'
16+
self.host_port='22'
17+
self.content=content
18+
19+
def ssh(self,exec_comm):
20+
self.ip = re.search('\[(.*?)\]', self.content).group(1)
21+
ssh = paramiko.SSHClient()
22+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
23+
ssh.connect(self.ip, self.host_port, self.user, self.password)
24+
stdin, stdout, stderr = ssh.exec_command(exec_comm)
25+
out = stdout.readlines()
26+
err = stderr.readlines()
27+
ssh.close()
28+
return out, err
29+
30+
return out, err
31+
def getmem(self):
32+
exec_comm='free -m'
33+
result=self.ssh(exec_comm)
34+
print(result)
35+
return JsonResponse(
36+
{"msgtype": "text",
37+
"text": {
38+
"content": result
39+
}
40+
}
41+
)
42+
43+
def getdisk(self):
44+
exec_comm='df -h'
45+
result=self.ssh(exec_comm)
46+
print(result)
47+
return JsonResponse(
48+
{"msgtype": "text",
49+
"text": {
50+
"content": result
51+
}
52+
}
53+
)
54+
def getport(self):
55+
port = re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2)
56+
exec_comm = "netstat -ntlp |egrep -w %s" % port
57+
result = self.ssh(exec_comm)
58+
print(result)
59+
return JsonResponse(
60+
{"msgtype": "text",
61+
"text": {
62+
"content": result
63+
}
64+
}
65+
)
66+
def getlog(self):
67+
logpath=re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2)
68+
exec_comm='tail -n 20 %s' %logpath
69+
result = self.ssh(exec_comm)
70+
print(result)
71+
return JsonResponse(
72+
{"msgtype": "text",
73+
"text": {
74+
"content": result
75+
}
76+
}
77+
)
78+
79+
def getservice(self):
80+
domain=re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2)
81+
exec_comm='systemctl status %s'%domain
82+
result = self.ssh(exec_comm)
83+
print(result)
84+
return JsonResponse(
85+
{"msgtype": "text",
86+
"text": {
87+
"content": result
88+
}
89+
}
90+
)
91+
92+
def getload(self):
93+
exec_comm = 'uptime'
94+
result = self.ssh(exec_comm)
95+
print(result)
96+
return JsonResponse(
97+
{"msgtype": "text",
98+
"text": {
99+
"content": result
100+
}
101+
}
102+
)
103+
def restart(self):
104+
service=re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2)
105+
exec_comm='systemctl restart %s'%service
106+
result = self.ssh(exec_comm)
107+
#return result
108+
return JsonResponse(
109+
{"msgtype": "text",
110+
"text": {
111+
"content": '重启结果'+str(result)
112+
}
113+
}
114+
)
115+
def shellres(content):
116+
shells=shell(content)
117+
shellreflect={'内存信息':'getmem','磁盘信息':'getdisk','日志信息':'getlog','服务检测':'getservice','端口检测':'getport','负载信息':'getload','重启服务':'restart'}
118+
for i in shellreflect.keys():
119+
120+
if '重启服务' in content:
121+
122+
split=content.split('|')
123+
print(split)
124+
if len(split)==2 and split[1]=='是':
125+
cache.delete('name')
126+
cache.delete('data')
127+
#del split
128+
return shells.restart()
129+
if len(split)==2 and split[1]=='否':
130+
cache.delete('name')
131+
cache.delete('data')
132+
#del split
133+
return JsonResponse(
134+
{"msgtype": "text",
135+
"text": {
136+
"content": '取消重启服务操作'
137+
}
138+
}
139+
)
140+
else :
141+
cache.set('data',content)
142+
print(cache.get('data'))
143+
return JsonResponse(
144+
{"msgtype": "text",
145+
"text": {
146+
"content": '确定要重启服务吗?(是/否)'
147+
}
148+
}
149+
)
150+
151+
if i in content:
152+
rest = getattr(shells, shellreflect[i])
153+
return rest()
154+
else:
155+
continue
156+
157+
158+
return JsonResponse(
159+
{"msgtype": "text",
160+
"text": {
161+
"content": "您输入的格式有错误,请重新输入"
162+
}
163+
}
164+
)
165+
166+
167+

0 commit comments

Comments
 (0)