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
|
Option Explicit
Dim fso, logObj
Const ForReading = 1
Const strLogFileName = "testlog.txt"
Const strTmpFileName = "Checklog.tmp"
Const strExcludePath = "exclude.txt"
Const strOutFileName = "wmicollectionOut.txt"
Const strINIFileName = "wmicollect.ini"
Const strJobName = "test"
Const bUseIntegrated = true
Const txtServer = "sgb"
Const JobStartTimeout = 120
Const strComplete = "Job Complete."
Const Subject = "WMI Collector is hung"
Const MsgBody = "Please fix"
Const SMTPTimeout = 10
Const FromAddress = "ghtools@microsoft.com"
Const ToAddress = "siggib@microsoft.com"
Const CCAddress = ""
Const cdoSendUsingPort = 2
Const MailServerName = "smarthost.dns.microsoft.com"
Const ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set logObj = fso.opentextfile("checklog.log",ForAppending,true)
Main
Sub Main()
Dim LogFileObj, strLine, Strlastfile, array1, array2,tmpFileObj, Success
If fso.fileexists(strtmpfilename) Then
Set tmpFileObj = fso.opentextfile(strtmpfilename)
If not tmpfileobj.atendofstream Then
strlastFile = tmpfileobj.readline
Else
strlastfile = "blank" & vbtab & "file"
End If
tmpfileobj.close
Else
strlastfile = "blank" & vbtab & "file"
End If
'writelog strlastfile
array2 = split(strlastfile,vbtab)
If Trim(array2(1)) = strComplete Then
writelog "Done"
wscript.quit
End If
If fso.FileExists(strlogfilename) Then
Set LogFileObj = fso.OpenTextFile(strlogFileName, ForReading)
Else
writelog "Logfile " & strlogfilename & " not found"
wscript.quit
End If
While not LogFileObj.atendofstream
strLine = LogFileObj.readline
Wend
array1=split(strline,vbtab)
If (Trim(array1(0)) = Trim(array2(0))) and (Trim(array1(1)) <> strComplete) Then
writelog "Collector hung, attempting to restart..."
writelog strline
UpdateFiles(Trim(array1(1)))
Success = RestartJob()
Select Case Success
Case 0
writelog "Successfully restarted Collector. Sending notification mail."
mysendmail "Collector hung, automatically restarted","Hung while attempting:" & vbcrlf & strline
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.close
Case 1
writelog "Collector is hung, unable to restart it. Hung while attempting to stop."
mysendmail subject,"Unable to automatically restart it. Job hung while attempting to stop" & vbcrlf & strline
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.writeline "Collector Hung" & vbtab & strComplete
tmpfileobj.close
Case 2
writelog "Collector is hung, unable to restart it. Didn't respond to start command."
mysendmail subject,"Unable to automatically restart it. Didn't respond to start command." & vbcrlf & strline
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.writeline "Collector Hung" & vbtab & strComplete
tmpfileobj.close
Case 3
writelog "Collector is hung, unable to restart it. Hung while attempting to start."
mysendmail subject,"Unable to automatically restart it. Hung while attempting to start." & vbcrlf & strline
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.writeline "Collector Hung" & vbtab & strComplete
tmpfileobj.close
Case 4
writelog "Collector is hung, unable to restart it. Failed to start."
mysendmail subject,"Unable to automatically restart it. Failed to start." & vbcrlf & strline
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.writeline "Collector Hung" & vbtab & strComplete
tmpfileobj.close
Case 5
writelog "Collector is hung, unable to restart it. SQL Agent stopped and was unable to restart it."
mysendmail subject,"Unable to automatically restart it. SQL Agent stopped and was unable to restart it." & vbcrlf & strline
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.writeline "Collector Hung" & vbtab & strComplete
tmpfileobj.close
End Select
writelog "Mail sent"
Else
writelog "Everything seems OK"
Set tmpfileobj=fso.createtextfile(strtmpfilename,true)
tmpfileobj.writeline strline
tmpfileobj.close
End If
End Sub
Sub UpdateFiles(logmsg)
Dim arr1, SrvName, ExcludeObj, OutFileObj, strLine, TargetId, INIFileObj, tmpfileobj
If fso.FileExists(strOutFileName) Then
Set OutFileObj = fso.OpenTextFile(strOutFileName, ForReading)
Else
writelog "Logfile " & strOutfilename & " not found"
wscript.quit
End If
writelog "Looking for current targetID"
While not OutFileObj.atendofstream
strLine = OutFileObj.readline
Wend
arr1=split(logmsg," ")
SrvName = Left(arr1(2),Len(arr1(2))-3)
arr1=split(strLine, vbtab)
TargetID = Trim(arr1(1))
writelog "Found it targetid of " & TargetID
'wscript.echo "ServerName=" & srvname
writelog "Updating exlude file..."
Set ExcludeObj=fso.OpenTextFile(strExcludePath,ForAppending,true)
ExcludeObj.writeline srvname
If fso.FileExists(strINIFileName) Then
Set INIFileObj = fso.OpenTextFile(strINIFileName, ForReading)
Set tmpfileobj = fso.createtextfile(strINIFileName & ".tmp",true)
Else
writelog "Logfile " & strOutfilename & " not found"
wscript.quit
End If
writelog "Updating ini file..."
While not INIFileObj.atendofstream
strLine = INIFileObj.readline
If LCase(Left(strline,5)) <> "start" Then tmpFileObj.writeline strline
Wend
tmpFileObj.writeline "startat = " & TargetID + 2
fso.copyfile strINIFileName & ".tmp", strINIFileName, true
End Sub
Sub MySendMail(StrSubject,msg)
Dim iMsg,iConf,Flds
Exit Sub
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailServerName
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = SMTPTimeout
.Update
End With
With iMsg
Set .Configuration = iConf
.To = ToAddress
.CC = CCAddress
.From = FromAddress
.Subject = StrSubject
.textbody = Msg
.Send
End With
End Sub
Sub writelog (msg)
wscript.echo msg
logObj.writeline Now & vbtab & msg
End Sub
Function RestartJob()
Dim Job, oSQLServer, sUsername, sPasswd, Status, sec1, dtStart
'wscript.echo "bUseIntegrated=" & bUseIntegrated
'wscript.echo "txtServer=" & txtServer
Set oSQLServer = CreateObject("SQLDMO.SQLServer")
oSQLServer.LoginTimeout = -1 '-1 is the ODBC default (60) seconds
'Connect to the Server
If bUseIntegrated Then
With oSQLServer
.LoginSecure = True 'Use NT Authentication
.AutoReConnect = False
.Connect txtServer
End With
Else
With oSQLServer
.LoginSecure = False 'Use SQL Server Authentication
.AutoReConnect = False
.Connect txtServer, sUsername, sPasswd
End With
End If
If oSQLServer.JobServer.Status <> 1 Then
writelog "SQLAgent not running, attempting to start"
oSQLServer.JobServer.start
oSQLServer.JobServer.Jobs.refresh(false)
dtstart = Now
Do While oSQLServer.JobServer.Status <> 1
sec1=Second(Now)
While sec1=Second(Now)
Wend
wscript.echo "Agent Status=" & oSQLServer.JobServer.Status
oSQLServer.JobServer.Jobs.refresh(false)
If DateAdd("s",JobStartTimeout,dtStart) <=Now Then
writelog "SQLAgent didn't start in time allowed"
oSQLServer.DisConnect
RestartJob = 5
Exit Function
End If
Loop
End If
wscript.echo "SQL Agent Started"
With oSQLServer.JobServer.Jobs(strJobName)
'wscript.echo .name & vbtab & .LastRunOutcome & vbtab & .LastRunDate
If .CurrentRunStatus <> 4 Then
writelog "Job currently running, attempting to stopp job."
.stop()
oSQLServer.JobServer.Jobs.refresh(false)
dtStart = Now
Do While .CurrentRunStatus <> 4
sec1=Second(Now)
While sec1=Second(Now)
Wend
wscript.echo "CurrentRunStatus=" & .CurrentRunStatus
oSQLServer.JobServer.Jobs.refresh(false)
'wscript.echo "JobStartTimeout=" & JobStartTimeout & "; dtStart=" & dtStart
'wscript.echo "JobStartTimeout + dtStart = " & DateAdd("s",JobStartTimeout,dtStart)
'wscript.echo "Now=" & Now
If DateAdd("s",JobStartTimeout,dtStart) <=Now Then
writelog "Job didn't stop in time allowed"
oSQLServer.DisConnect
RestartJob = 1
Exit Function
End If
Loop
writelog "Now restarting job"
.start
Else
writelog "Collector not running, attempting to start the collector"
.start
End If
oSQLServer.JobServer.Jobs.refresh(false)
'sec1=Second(Now)
'While sec1<Second(Now)+5
'Wend
dtStart = Now
Do While .CurrentRunStatus = 4
oSQLServer.JobServer.Jobs.refresh(false)
sec1=Second(Now)
While sec1=Second(Now)
Wend
wscript.echo "CurrentRunStatus=" & .CurrentRunStatus
oSQLServer.JobServer.Jobs.refresh(false)
'wscript.echo "JobStartTimeout=" & JobStartTimeout & "; dtStart=" & dtStart
'wscript.echo "JobStartTimeout + dtStart = " & DateAdd("s",JobStartTimeout,dtStart)
'wscript.echo "Now=" & Now
If DateAdd("s",JobStartTimeout,dtStart) <=Now Then
writelog "Job didn't start in time allowed"
oSQLServer.DisConnect
RestartJob = 2
Exit Function
End If
Loop
oSQLServer.JobServer.Jobs.refresh(false)
dtStart = Now
Do While .CurrentRunStatus <> 1
oSQLServer.JobServer.Jobs.refresh(false)
sec1=Second(Now)
While sec1=Second(Now)
Wend
wscript.echo "CurrentRunStatus=" & .CurrentRunStatus
oSQLServer.JobServer.Jobs.refresh(false)
If .CurrentRunStatus = 4 Then
wscript.echo "Job stopped"
Exit Do
End If
If DateAdd("s",JobStartTimeout,dtStart) <=Now Then
writelog "Job didn't start in time allowed"
oSQLServer.DisConnect
RestartJob = 3
Exit Function
End If
Loop
oSQLServer.JobServer.Jobs.refresh(false)
If .CurrentRunStatus = 1 Then
writelog "Job started Successfully"
RestartJob = 0
Else
writelog "Job Failed to start"
RestartJob = 4
End If
End With
If Not oSQLServer Is Nothing Then
'When done with the connection to SQLServer you must Disconnect
oSQLServer.DisConnect
End If
End Function
|