windows平台服务监控邮件报警批处理脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32711825/article/details/89394694

目前项目缺少运维人员,作为JAVA研发,只能硬着头皮上。

批处理定义:bat全称batch,顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD。这些命令统称批处理命令。

一、脚本目录
在这里插入图片描述
1.blat.dll、blat.exe :发邮件的插件

Blat 是一个命令行发邮件的小工具,仅支持简单的SMTP协议,需要SMTP服务器的支持。
据说现在新申请的邮箱已经不支持SMTP服务了,是为了抑制垃圾邮件而采取的措施。
但是,Blat也是一个非常方便的小工具,基本上可以满足一般人的需求。

2.sendmail.bat: 发邮件脚本

Blat -body "转写机引擎异常,请立即查看,详情见附件" -s " 告警!引擎服务停止" -t ***@iflytek.com -server ***.iflytek.com -f ***@iflytek.com -u ***@iflytek.com -pw ***** -attach log.txt

命令参数说明:

邮件内容:转写机引擎异常,请立即查看,详情见附件 ;
邮件主题:告警!引擎服务停止;
收件人:***@iflytek.com ;
发件人服务器:***.iflytek.com ;
发件人:***@iflytek.com ;
账号:***@iflytek.com ;
密码:******;
附件 log.txt
更多功能在这行命令的基础上去优化完善即可;

3.startWatch.bat :监控脚本的启动入口

@echo off   
if "%1" == "h" goto begin 
    mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin 

set issend=0
:RESTART
tasklist  |find /C "进程名称" > task.txt
set /p num= < task.txt
del /F task.txt
echo start======num=%num%,issend=%issend%

if %num% == 0 if %issend% == 0 ( 
systeminfo > log.txt
tasklist >> log.txt
call sendmail.bat
 ) 

if %num% == 0 (
	set issend=1
) else (
	set issend=0
)

echo end=====num=%num%,issend=%issend%
ping -n 10 -w 2000 0.0.0.1> task.txt
del /F task.txt
goto RESTART

ping -n 10 -w 2000 0.0.0.1> task.txt
ping命令自带的延迟参数-w,将IP地址设置成一个空地址,比如0.0.0.1,这样该命令会一直ping,直到一定n*w毫秒后才返回(n和w分别是参数-n和-w指定的值),达到每隔一段时间去检测进程是否在运行的功能

4.log.txt :邮件的日志附件


主机名:           ZHANGJUN-PC
OS 名称:          Microsoft Windows 7 企业版 
OS 版本:          6.1.7601 Service Pack 1 Build 7601
OS 制造商:        Microsoft Corporation
OS 配置:          独立工作站
OS 构件类型:      Multiprocessor Free
注册的所有人:     zhangjun
注册的组织:       
产品 ID:          55041-051-1116642-86289
初始安装日期:     2018/5/24, 8:52:47
系统启动时间:     2019/4/8, 11:32:08
系统制造商:       VMware, Inc.
系统型号:         VMware Virtual Platform
系统类型:         x64-based PC
处理器:           安装了 1 个处理器。
                  [01]: Intel64 Family 6 Model 94 Stepping 3 GenuineIntel ~2591 Mhz
BIOS 版本:        Phoenix Technologies LTD 6.00, 2015/7/2
Windows 目录:     C:\Windows
系统目录:         C:\Windows\system32
启动设备:         \Device\HarddiskVolume1
系统区域设置:     zh-cn;中文(中国)
输入法区域设置:   zh-cn;中文(中国)
时区:             (UTC+08:00)北京,重庆,香港特别行政区,乌鲁木齐
物理内存总量:     4,095 MB
可用的物理内存:   2,755 MB
虚拟内存: 最大值: 8,189 MB
虚拟内存: 可用:   6,084 MB
虚拟内存: 使用中: 2,105 MB
页面文件位置:     C:\pagefile.sys
域:               WORKGROUP
登录服务器:       \\ZHANGJUN-PC
修补程序:         安装了 3 个修补程序。
                  [01]: KB2534111
                  [02]: KB958488
                  [03]: KB976902
网卡:             安装了 3 个 NIC。
                  [01]: Intel(R) PRO/1000 MT Network Connection
                      连接名:      本地连接
                      启用 DHCP:   是
                      DHCP 服务器: 172.16.215.254
                      IP 地址
                        [01]: 172.16.215.215
                        [02]: fe80::c5df:8d83:bcaf:d9bb
                  [02]: Bluetooth 设备(个人区域网)
                      连接名:      Bluetooth 网络连接
                      状态:        媒体连接已中断
                  [03]: Hillstone Virtual Network Adapter
                      连接名:      本地连接 2
                      状态:        媒体连接已中断

映像名称                       PID 会话名              会话#       内存使用 
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
System                           4 Services                   0      1,616 K
smss.exe                       292 Services                   0        160 K
csrss.exe                      380 Services                   0      2,140 K
wininit.exe                    464 Services                   0        336 K
svchost.exe                    956 Services                   0      6,980 K
svchost.exe                    988 Services                   0     27,756 K
svchost.exe                    476 Services                   0     11,392 K
svchost.exe                    328 Services                   0     11,764 K
kxescore.exe                  1104 Services                   0     47,504 K
spoolsv.exe                   1400 Services                   0      4,600 K
svchost.exe                   1440 Services                   0     13,504 K
svchost.exe                   1532 Services                   0     46,596 K
vmtoolsd.exe                  1756 Services                   0      8,076 K
svchost.exe                   2248 Services                   0        284 K
msdtc.exe                     2672 Services                   0      1,800 K
WmiPrvSE.exe                  2792 Services                   0     13,820 K
taskhost.exe                  2920 Console                    1      5,520 K
dwm.exe                       3036 Console                    1     85,212 K
explorer.exe                  3064 Console                    1    194,896 K
vmtoolsd.exe                  3144 Console                    1     19,768 K
TSVNCache.exe                 3332 Console                    1      3,816 K
SearchIndexer.exe             3256 Services                   0     36,320 K
svchost.exe                   5764 Services                   0     21,420 K
EA.exe                        8176 Console                    1     31,224 K
SogouCloud.exe                6820 Console                    1     12,804 K
wpscenter.exe                 6548 Console                    1     16,832 K

5.task.txt:脚本执行过程中生成的过渡文件,没有实际用途,仅当做日志查看


正在 Ping 0.0.0.1 具有 32 字节的数据:
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 
PING: 传输失败。General failure. 

0.0.0.1 的 Ping 统计信息:
    数据包: 已发送 = 8,已接收 = 0,丢失 = 8 (100% 丢失),

6.readme.txt

####改脚本完成Windows的撰写机引擎的监控任务,并进行邮件报警########

1、在startWatch.bat中修改进程名称为你需要监控的进程名称,如qq.exe。
2、在sendmail.bat中将邮箱修改成实际的邮箱和密码 。
3、双击运行当前目录的startWatch.bat即可

########如有疑问,请联系junzhang27#######

二、脚本执行效果
在这里插入图片描述

三、脚本回显
需要将startWatch.bat中以下代码注释掉才能不关闭dos窗口

@echo off   
if "%1" == "h" goto begin 
    mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin    

在这里插入图片描述
四、脚本代码
https://note.youdao.com/ynoteshare1/index.html?id=6e13bf4482faf78ad25cd850cc518a0e&type=note

五、参考文档
https://blog.51cto.com/mflag/2283256
https://edu.51cto.com/center/course/lesson/index?id=328180
https://blog.51cto.com/1870707/1296601

猜你喜欢

转载自blog.csdn.net/qq_32711825/article/details/89394694
今日推荐