健康状态监测bat脚本-ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台
记录工作点滴
分享运维知识

健康状态监测bat脚本

前言

某qlik sense服务器需要一个health check(健康状态监测)的程序,所以就有了一下的批处理脚本。

大致功能如下:

  • 批处理获取当前日期时间
  • 批处理生成动态的文件名
  • 批处理输出echo到日志文件
  • 批处理遍历列表list
  • 批处理函数与call函数
  • 判断服务是否运行中
  • 批处理count计数
  • 批处理copy复制文件
  • 批处理判断大于gtr(记住那辆跑车名,批处理大于、小于、等于、不小于、不大于和不等于)
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
@echo off
color 2E
set YYYYmmdd=%date:~0,4%%date:~5,2%%date:~8,2%
set hhmiss=%time:~0,2%%time:~3,2%%time:~6,2%
set filename=healthcheck.log
REM set "filename=healthcheck_%YYYYmmdd%_%hhmiss%.log"
set "datetime=%YYYYmmdd%%hhmiss%"
set allCount=0
set errorCount=0
set checkList="QlikSenseServiceDispatcher" "QlikSenseSchedulerService" "QlikSenseRepositoryService" "QlikSenseRepositoryDatabase" "QlikSenseProxyService" "QlikSensePrintingService" "QlikSenseEngineService" "QlikLoggingService"
echo ============================================ >%filename%
echo Health Checking by Qlik Team         >>%filename%
echo zhengkai.blog.csdn.net
echo (datetime: %datetime% )            >>%filename%
echo ============================================ >>%filename%
echo.                       >>%filename%
echo.                       >>%filename%
for %%L in (%checkList%) do call :health_check %%L
echo ============================================ >>%filename%
echo Checking Finish.               >>%filename%
echo.                       >>%filename%
echo Error : %errorCount% of %allCount%      >>%filename%
echo.                       >>%filename%
echo ============================================ >>%filename%
call :check_status                
echo ============================================ >>%filename%
goto:end
:health_check
sc query |find /i %1 >nul 2>nul
if not errorlevel 1 (
 echo SUCCESS...... %1             >>%filename%
)else (
 echo FAILD...... %1 is error !         >>%filename%
 set /a errorCount+=1
)
set /a allCount+=1
goto:eof
:check_status
if %errorCount% gtr 0 (
 echo ###### this server is Not Health     >>%filename%
) else (
 echo ###### this server is Health       >>%filename%
)
goto:eof
:end
copy %filename% <a href="file://\\xxxx\DataShare\CSDN\ZhengKai\">\\xxxx\DataShare\CSDN\ZhengKai\</a>

批处理获取当前日期时间,生成动态的文件名

1
2
3
4
set YYYYmmdd=%date:~0,4%%date:~5,2%%date:~8,2%
set hhmiss=%time:~0,2%%time:~3,2%%time:~6,2%
set "filename=healthcheck_%YYYYmmdd%_%hhmiss%.log"
echo %filename%

批处理输出echo到日志文件

  • > 就是清空输出文件,重新输出层
  • >> 就是追加到日子和文件
1
2
3
echo ============================================ >%filename%
echo Health Checking by Qlik Team         >>%filename%
echo ============================================ >>%filename%

批处理遍历列表list

定义在一个List会后,就可以开始使用

  • for %%L 用什么变量来遍历
  • in (%checkList%) 遍历哪个数组
  • do echo %%L 遍历来做什么
1
2
3
4
5
set checkList="QlikSenseServiceDispatcher" "QlikSenseSchedulerService" "QlikSenseRepositoryService" "QlikSenseRepositoryDatabase" "QlikSenseProxyService" "QlikSensePrintingService" "QlikSenseEngineService" "QlikLoggingService"
echo ============================================ >%filename%
echo Health Checking List             >>%filename%
for %%L in (%checkList%) do echo %%L
echo ============================================ >>%filename%

批处理函数与call函数

定义一个:function_name,中间是函数内容,后面以goto:eof结尾

1
2
3
4
5
6
7
8
9
call :check_status
:check_status
if %errorCount% gtr 0 (
 echo ###### this server is Not Health     >>%filename%
) else (
 echo ###### this server is Health       >>%filename%
)
goto:eof

批处理count计数

计数加一 set /a allCount+=1

1
2
set allCount=0
for %%L in (%checkList%) do set /a allCount+=1

批处理copy复制文件

  • 复制文件 copy 文件名 目录页
  • 复制文件夹用xcopy,指定的目录连文件和目录结构一并拷贝

copy %filename% “\\xxxx\DataShare\CSDN\ZhengKai\

批处理大于、小于、等于、不小于、不大于和不等于

  • EQU – 等于
  • NEQ – 不等于
  • LSS – 小于
  • LEQ – 小于或等于
  • GTR – 大于
  • GEQ – 大于或等于
1
2
3
4
5
if %errorCount% gtr 0 (
 echo ###### this server is Not Health     >>%filename%
) else (
 echo ###### this server is Health       >>%filename%
未经允许不得转载:ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台 » 健康状态监测bat脚本

如果文章对你有帮助,欢迎点击上方按钮打赏作者

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址