File size: 13,372 Bytes
020c337 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | #coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2020 宝塔软件(https://www.bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: 沐落 <cjx@bt.cn>
# | Author: lx
# +-------------------------------------------------------------------
import sys,os,time,psutil,re
panelPath = "/www/server/panel"
os.chdir(panelPath)
sys.path.insert(0, "class/")
import public,time,panelPush,public
try:
from BTPanel import cache
except :
from cachelib import SimpleCache
cache = SimpleCache()
class panel_push:
__push = None
pids = None
def __init__(self):
self.__push = panelPush.panelPush()
#-----------------------------------------------------------start 添加推送 ------------------------------------------------------
def get_version_info(self,get):
"""
获取版本信息
"""
data = {}
data['ps'] = ''
data['version'] = '1.2'
data['date'] = '2022-09-20'
data['author'] = '宝塔'
data['help'] = 'http://www.bt.cn/bbs'
return data
#名取PID
def getPid(self,pname):
try:
if not self.pids: self.pids = psutil.pids()
for pid in self.pids:
if psutil.Process(pid).name() == pname: return True
return False
except: return True
#检测指定进程是否存活
def checkProcess(self,pid):
try:
if not self.pids: self.pids = psutil.pids()
if int(pid) in self.pids: return True
return False
except:
return False
#检查是否启动
def check_run(self,name):
if name == "php-fpm":
status = False
base_path = "/www/server/php"
if not os.path.exists(base_path):
return status
for p in os.listdir(base_path):
pid_file = os.path.join(base_path, p, "var/run/php-fpm.pid")
if os.path.exists(pid_file):
php_pid = int(public.readFile(pid_file))
status = self.checkProcess(php_pid)
if status:
return status
return status
elif name == 'nginx':
status = False
if os.path.exists('/etc/init.d/nginx'):
pidf = '/www/server/nginx/logs/nginx.pid'
if os.path.exists(pidf):
try:
pid = public.readFile(pidf)
status = self.checkProcess(pid)
except:
pass
return status
elif name == 'apache':
status = False
if os.path.exists('/etc/init.d/httpd'):
pidf = '/www/server/apache/logs/httpd.pid'
if os.path.exists(pidf):
pid = public.readFile(pidf)
status = self.checkProcess(pid)
return status
elif name == 'mysql':
res = public.ExecShell("service mysqld status")
if res and not re.search("not\s+running", res[0]):
return True
return False
elif name == 'tomcat':
status = False
if os.path.exists('/www/server/tomcat/logs/catalina-daemon.pid'):
if self.getPid('jsvc'): status = True
if not status:
if self.getPid('java'): status = True
return status
elif name == 'pure-ftpd':
pidf = '/var/run/pure-ftpd.pid'
status = False
if os.path.exists(pidf):
pid = public.readFile(pidf)
status = self.checkProcess(pid)
return status
elif name == 'redis':
status = False
pidf = '/www/server/redis/redis.pid'
if os.path.exists(pidf):
pid = public.readFile(pidf)
status = self.checkProcess(pid)
return status
elif name == 'memcached':
status = False
pidf = '/var/run/memcached.pid'
if os.path.exists(pidf):
pid = public.readFile(pidf)
status = self.checkProcess(pid)
return status
return True
def get_server_status(self, server_name):
status = self.check_run(server_name)
if status:
return 1
return 0
"""
@获取推送模块配置
"""
def get_module_config(self,get):
data = []
item = self.__push.format_push_data(push = ["mail",'dingding','weixin',"feishu"],project = 'ssl',type = 'ssl')
item['cycle'] = 30
item['title'] = 'SSL到期提醒'
data.append(item)
item = self.__push.format_push_data(push = ["mail",'dingding','weixin',"feishu"],project = 'endtime',type = 'endtime')
item['cycle'] = 5
item['title'] = '专业版/企业版到期提醒'
item['helps'] = ['']
data.append(item)
services = ['nginx','apache',"pure-ftpd",'mysql','php-fpm','memcached','redis']
channels = ['mail', 'dingding','weixin','sms',"feishu"]
for x in services:
item = {}
item['project'] = x
item['title'] = '{}服务停止通知'.format(x)
if x == 'other':
item['title'] = '自定义服务停止通知'
else:
ser_name = self.__get_service_name(x)
if self.get_server_status(ser_name) == -1: continue
item['type'] = 'services'
item['keys'] = []
item['interval'] = 300
for x in ['stop']:
item['keys'].append({'key':x,'val':'停止'})
item['push'] = channels
item['helps'] = ['部分服务停止可能会造成业务中断.','短信推送需提前购买,如需购买请联系[<a href="http://q.url.cn/CDfQPS?_type=wpa&qidian=true" target="_blank" class="btlink">宝塔客服</a>]']
data.append(item)
import json
public.writeFile('data/push/config/panel_push.json',json.dumps(data))
return data
def get_push_cycle(self,data):
"""
@获取执行周期
"""
result = {}
for skey in data:
result[skey] = data[skey]
m_cycle =[]
m_type = data[skey]['type']
if m_type in ['endtime','ssl']:
m_cycle.append('剩余{}天时,每天1次'.format(data[skey]['cycle']))
elif m_type in ['services']:
m_cycle.append('服务停止时,每{}秒1次'.format(data[skey]['interval']))
if len(m_cycle) > 0:
result[skey]['m_cycle'] = ''.join(m_cycle)
return result
#-----------------------------------------------------------end 添加推送 ------------------------------------------------------
"""
@获取服务真实名称
"""
def __get_service_name(self,name):
slist = {"FTP服务端":'pure-ftpd'}
if name in slist: name = slist[name]
return name
"""
@获取推送栏目
"""
def get_total(self):
return True;
def get_push_data(self,data,total):
if data['type'] == 'services':
ser_name = data['project']
ser_name = self.__get_service_name(ser_name)
status = self.get_server_status(ser_name)
if status > 0:
return public.returnMsg(False,"状态正常,跳过.")
else:
if status == 0:
return self.__get_service_result(data)
return public.returnMsg(False,"服务未安装,跳过.")
elif data['type'] in ['ssl']:
if time.time() < data['index'] + 86400:
return public.returnMsg(False,"一天推送一次,跳过.")
import panelSSL
ssl = panelSSL.panelSSL()
clist = []
for x in ssl.GetCertList(None):
timeArray = time.strptime(x['notAfter'], "%Y-%m-%d")
endtime = time.mktime(timeArray)
day = int((endtime - time.time()) / 86400)
if day > data['cycle']: continue
clist.append(x)
return self.__get_ssl_result(data,clist)
elif data['type'] in ['endtime']:
if time.time() < data['index'] + 86400:
return public.returnMsg(False,"一天推送一次,跳过.")
from pluginAuth import Plugin
softs = Plugin(False).get_plugin_list(True)
if softs['pro'] == 0: return public.returnMsg(False,"永久专业版,跳过.")
if softs['ltd'] == -2 and softs['pro'] == -2:
pass
else:
pro_data = {}
if softs['ltd'] > 0:
pro_data['endtime'] = softs['ltd']
pro_data['name'] = "Linux企业版"
pro_data['affect'] = '全部企业版插件'
elif softs['pro'] > 0:
pro_data['endtime'] = softs['pro']
pro_data['name'] = "Linux专业版"
pro_data['affect'] = '全部专业版插件'
pro_data['day'] = int((pro_data['endtime'] - time.time()) / 86400)
if pro_data['day'] <= data['cycle']:
return self.__get_ltd_result(data,pro_data)
return public.returnMsg(False,"未达到阈值,跳过.")
"""
@企业版到期提醒
"""
def __get_ltd_result(self,data,pro_data):
result = {'index':time.time()}
for m_module in data['module'].split(','):
result[m_module] = self.__push.format_msg_data()
newline = ""
if m_module in ['dingding','weixin',"feishu","mail"]:
if m_module in ["mail"]:
newline="<br/>"
result[m_module]["title"] = "宝塔面板业务到期提醒"
else:
newline = "\n\n"
result[m_module]['msg'] = "".join((
"#### 宝塔面板业务到期提醒"+newline,
">服务器 :"+ public.GetLocalIp() + newline,
">剩余天数:"+ str(pro_data['day'] + 1) +" 天"+newline,
">到期产品:"+ pro_data['name'] +newline,
">到期时间:"+ public.format_date(times =pro_data['endtime']) +newline,
">影响业务:"+ pro_data['affect'] +newline,
">通知时间:" + public.format_date() + newline))
return result
"""
@ssl到期返回
"""
def __get_ssl_result(self,data,clist):
if len(clist) == 0:
return public.returnMsg(False,"未找到到期证书,跳过.")
result = {'index':time.time() }
for m_module in data['module'].split(','):
result[m_module] = self.__push.format_msg_data()
newline = ""
if m_module in ['dingding','weixin',"feishu","mail"]:
if m_module in ["mail"]:
newline="<br/>"
result[m_module]["title"] = "宝塔面板SSL到期提醒"
else:
newline = "\n\n"
p_msg = "";
for x in clist: p_msg+= " 到期:{} 域名:{}".format(x['notAfter'],x['subject']) + newline
result[m_module]['msg'] ="".join((
"#### 宝塔面板SSL到期提醒" + newline,
">服务器 :"+ public.GetLocalIp() +newline,
">检测时间:" + public.format_date() +newline,
">即将到期:"+ str(len(clist)) +" 张"+newline,
p_msg))
return result
"""
@服务停止返回
"""
def __get_service_result(self,data):
s_idx = int(time.time())
if s_idx < data['index'] + data['interval']:
return public.returnMsg(False,"未达到间隔时间,跳过.")
result = {'index':s_idx}
for m_module in data['module'].split(','):
result[m_module] = self.__push.format_msg_data()
newline = ""
if m_module in ['dingding','weixin',"feishu","mail"]:
if m_module in ["mail"]:
newline="<br/>"
result[m_module]["title"] = "堡塔服务停止告警"
else:
newline = "\n\n"
result[m_module]['msg'] = "".join((
"#### 堡塔服务停止告警" + newline,
">服务器 :"+ public.GetLocalIp() +newline + " ",
">服务类型:"+ data["project"] +newline + " ",
">服务状态:已停止"+newline+" ",
">检测时间:"+ public.format_date()))
elif m_module in ['sms']:
result[m_module]['sm_type'] = 'servcies'
result[m_module]['sm_args'] = { 'name':'{}'.format(public.GetConfigValue('title')), 'product':data["project"],'product1':data["project"]}
return result
|