From 316ddc717e221cc3872d99420c6c71c110cc066f Mon Sep 17 00:00:00 2001 From: yaokl Date: Sat, 28 Jan 2023 10:25:09 +0800 Subject: [PATCH] feat: add scripts --- powershell/checkfile-email.ps1 | 53 ++++++++++++++++++++++++++++++++ powershell/checkfile-wechat.ps1 | 29 +++++++++++++++++ python/supervisor_healthCheck.py | 43 ++++++++++++++------------ python/supervisor_healthCheck.sh | 22 +++++++++++++ shell/test/os_repo_speed_test.sh | 2 +- 5 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 powershell/checkfile-email.ps1 create mode 100644 powershell/checkfile-wechat.ps1 create mode 100644 python/supervisor_healthCheck.sh diff --git a/powershell/checkfile-email.ps1 b/powershell/checkfile-email.ps1 new file mode 100644 index 0000000..198d81b --- /dev/null +++ b/powershell/checkfile-email.ps1 @@ -0,0 +1,53 @@ +function sendmail() +{ + function mailoptions($mailaddr,$body) #¶¨Òå·¢ËÍÓʼþµÄ·½·¨ + { + $msg=New-Object System.Net.Mail.MailMessage + $msg.To.Add($mailaddr) + $msg.From = New-Object System.Net.Mail.MailAddress("notice@test.com", "±¸·Ýµã¼ìÔ±",[system.Text.Encoding]::GetEncoding("UTF-8")) #·¢¼þÈË + $msg.Subject = "Windows±¸·Ýʧ°Ü֪ͨ£¡" + $msg.SubjectEncoding = [system.Text.Encoding]::GetEncoding("UTF-8") + $msg.Body =$body + $msg.BodyEncoding = [system.Text.Encoding]::GetEncoding("UTF-8") + $msg.IsBodyHtml = $false #·¢ËÍhtml¸ñʽÓʼþ + $client = New-Object System.Net.Mail.SmtpClient("smtp.qiye.163.com") #ÅäÖÃsmtp·þÎñÆ÷ + $client.Port = 25 #Ö¸¶¨smtp¶Ë¿Ú + $client.UseDefaultCredentials = $false + $client.Credentials=New-Object System.Net.NetworkCredential("notice@test.com", "xxxxxxxx") + try {$client.Send($msg)} + catch [Exception] + {$($_.Exception.Message) + $mailaddr + } + } + + $tomailaddr = "ops@test.com" + mailoptions $tomailaddr $Emailbody +} + + + +$Emailbody= "Dear All : +" + +$counter= 0 + +#test±¸·Ý¼ì²é +if (Get-ChildItem D:\serverbackup\WindowsImageBackup\test\Catalog | Where{$_.LastWriteTime -lt (Get-Date).AddDays(-32)}) +{ + $Emailbody= $Emailbody + + " + ¡ð test£¨10.8.8.1£©£º + " + $counter= $counter + 1 +} + +$Emailbody= $Emailbody + +" + ¸æ¾¯À´Ô´ 127.0.0.1 +" + +if ($counter -gt 0) +{ + sendmail +} diff --git a/powershell/checkfile-wechat.ps1 b/powershell/checkfile-wechat.ps1 new file mode 100644 index 0000000..df13bf4 --- /dev/null +++ b/powershell/checkfile-wechat.ps1 @@ -0,0 +1,29 @@ +function wechattalk() +{ + curl -Method Get "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx" +} + +$msg= "Windows Server±¸·Ýʧ°Ü֪ͨ£¡ +" +$counter= 0 + +#±¸·Ý¼ì²é +if (Get-ChildItem D:\serverbackup\WindowsImageBackup\test\Catalog | Where{$_.LastWriteTime -lt (Get-Date).AddDays(-32)}) +{ + $msg= $msg + + " + ¡ð test£¨10.8.8.1£©£º + " + $counter= $counter + 1 +} + +if ($counter -eq 0){ + $msg= "Windows Server ±¸·Ý¼ì²éÍê³É£¬±¸·Ý״̬Õý³££¡" +} + +$msg= $msg + +" + ¸æ¾¯À´Ô´ 127.0.0.1 +" + +wechattalk \ No newline at end of file diff --git a/python/supervisor_healthCheck.py b/python/supervisor_healthCheck.py index d8ab06d..3acddf6 100644 --- a/python/supervisor_healthCheck.py +++ b/python/supervisor_healthCheck.py @@ -524,7 +524,7 @@ class HealthCheck(object): restart_result = self.action_supervisor_restart(program) msg += '\r\n**Restart**:%s' % restart_result elif 'exec' in action_list: - action_exec_cmd = config.get('action_exec_cmd') + action_exec_cmd = config.get('execCmd') exec_result = self.action_exec(program, action_exec_cmd) msg += '\r\n**Exec**:%s' % exec_result elif 'kill' in action_list: @@ -712,26 +712,31 @@ class HealthCheck(object): touser = self.wechat_config.get('touser') toparty = self.wechat_config.get('toparty') totag = self.wechat_config.get('totag') + webhook = self.wechat_config.get('webhook') headers = { 'Content-Type': 'application/json' } - access_token_url = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format(id=corpid, crt=secret) - - try: - httpClient = httplib.HTTPSConnection(host, timeout=10) - httpClient.request("GET", access_token_url, headers=headers) - response = httpClient.getresponse() - token = json.loads(response.read())['access_token'] - except Exception as e: - self.log(program, '[Action: wechat] get token error %s' % e) - return False - finally: - if httpClient: - httpClient.close() - - send_url = '/cgi-bin/message/send?access_token={token}'.format(token=token) + if corpid and secret and agentid: + access_token_url = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format(id=corpid, crt=secret) + + try: + httpClient = httplib.HTTPSConnection(host, timeout=10) + httpClient.request("GET", access_token_url, headers=headers) + response = httpClient.getresponse() + token = json.loads(response.read())['access_token'] + except Exception as e: + self.log(program, '[Action: wechat] get token error %s' % e) + return False + finally: + if httpClient: + httpClient.close() + + send_url = '/cgi-bin/message/send?access_token={token}'.format(token=token) + + if webhook: + send_url = webhook.replace('https://qyapi.weixin.qq.com','') ip = "" s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -766,11 +771,11 @@ class HealthCheck(object): data = { "msgtype": 'markdown', - "agentid": agentid, - "markdown": {'content': content}, - "safe": 0 + "markdown": {'content': content} } + if agentid: + data['agentid'] = agentid if touser: data['touser'] = touser if toparty: diff --git a/python/supervisor_healthCheck.sh b/python/supervisor_healthCheck.sh new file mode 100644 index 0000000..ee0bde1 --- /dev/null +++ b/python/supervisor_healthCheck.sh @@ -0,0 +1,22 @@ +mkdir /etc/supervisord.d/scripts + +cat < /etc/supervisord.d/healthCheck.ini +[program:healthCheck] +command=/etc/supervisord.d/scripts/supervisor_healthCheck.py +process_name=%(program_name)s +numprocs=1 +directory=/etc/supervisord.d/scripts/ +autostart=true +startsecs=2 +startretries=5 +autorestart=true +stopsignal=TERM +stopwaitsecs=3 +stopasgroup=true +killasgroup=true +user=root +redirect_stderr=true +stdout_logfile=/var/log/supervisor/healthCheck.log +stdout_logfile_maxbytes=200MB +stdout_logfile_backups=10 +EOF \ No newline at end of file diff --git a/shell/test/os_repo_speed_test.sh b/shell/test/os_repo_speed_test.sh index 12c6e44..6591902 100644 --- a/shell/test/os_repo_speed_test.sh +++ b/shell/test/os_repo_speed_test.sh @@ -28,7 +28,7 @@ mirrors=( [edu_tsinghua]="https://mirrors.tuna.tsinghua.edu.cn/" [sohu]="http://mirrors.sohu.com/" [huawei]="https://mirrors.huaweicloud.com/" - [netease]="http://mirrors.cn99.com/" + [netease]="https://mirrors.163.com/" [tencent]="https://mirrors.cloud.tencent.com/" [aliyun]="https://mirrors.aliyun.com/" )