468,110 Members | 1,556 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

HTA CPU Usage monitor

115 100+
This is an HTML application that allows you to monitor the CPU Usage (as seen in task manager) for any one task either on the local computer or a remote computer using vbscript. It then alerts you when that process has gone idle. I put it together this morning because I was tired of starting a program on remote computer, and continuously reconnect to see if the job was finished. This code could easily be modified into a full fledged “Task Manager” for a remote computer..in fact..i think I might just do that. I will, and I will make a step by step article for it. Because I’m bored.

(You need a username and password for the remote computer)
(copy this code into a blank text document and save it with the extension ".hta")

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Teh Waiter</title>
  5. <HTA:APPLICATION id="prjIS" MINIMIZEBUTTON="yes" MAXIMIZEBUTTON="no" INNERBORDER="yes" BORDER="thin" SINGLEINSTANCE="yes" APPLICATIONNAME="Waiting Script">
  6. <SCRIPT language="VBScript">
  7. <!--
  8.  
  9. Sub Window_onLoad
  10.  
  11. Dim objWMISer, objItem
  12. Dim colItems
  13. Dim intLeft, intTop
  14.  
  15. Set objWMISer = GetObject("winmgmts:\\.\root\cimv2")
  16. Set colItems = objWMISer.ExecQuery("Select * From Win32_DesktopMonitor")
  17.  
  18. 'Set Window Size
  19. For Each objItem in colItems
  20. intWidth = objItem.ScreenWidth
  21. intHeight = objItem.ScreenHeight
  22. Exit For 
  23. Next
  24.  
  25. intLeft = (intWidth - 385) / 2
  26. intTop = (intHeight - 230) / 2
  27. window.resizeTo 385,230
  28. window.MoveTo intLeft, intTop 
  29. End Sub
  30.  
  31.  
  32. Function IsValidIp(sIpAddress)'Well..is it?
  33.  
  34. Dim aTmp
  35. IsValidIp = False
  36. aTmp = split(sIpAddress,".")
  37.  
  38. If UBound(aTmp) <> 3 Then Exit Function
  39. For Each field In aTmp
  40. If field > 255 Then Exit Function
  41. Next
  42. IsValidIp = True
  43.  
  44. End Function
  45.  
  46.  
  47. 'This was complicated..
  48. 'Sets cursor to end of text when focus is gained
  49. Sub txtIP_OnFocus 
  50. Set it = document.Selection.CreateRange()
  51. it.moveStart "Character", 15
  52. it.select
  53.  
  54. End Sub
  55.  
  56. Dim sObjectPath, strIPAddress, strUser, strPass, strName
  57. Dim blnIdle, blnIsDone, blnFocus
  58. Dim intWait, intTime
  59. Dim wmi_service, perf_instance1
  60. Dim N1, N2, D1, D2
  61. Dim TimerID, TimerWork
  62.  
  63. Sub btnDoIt_OnClick
  64. document.getElementById("btnDoIt").disabled = "disabled"
  65.  
  66. strIPAddress = document.getElementById("txtIP").value
  67. strUser = "remoteuser"
  68. strPass = "secretpassword"
  69. strName = document.getElementById("txtName").value
  70. intTime = CInt(document.getElementById("txtTime").value)
  71.  
  72. blnIsDone = false
  73. blnIdle = false
  74.  
  75. intWait = 0
  76.  
  77. If trim(strName) = "" Then
  78. msgbox "Enter a process to wait for!"
  79. document.getElementById("btnDoIt").disabled = ""
  80. Exit Sub
  81. End If
  82.  
  83. sObjectPath = "Win32_PerfRawData_PerfProc_Process.Name=" & chr(34) & strName & chr(34)
  84.  
  85. If strIPAddress = "." Then
  86. Set wmi_service = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
  87. Else 
  88. If IsValidIp(strIPAddress) Then
  89. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  90. Set wmi_service = objSWbemLocator.ConnectServer(strIPAddress, "root\cimv2", strUser, strPass)
  91. Else
  92. msgbox "Invalid Ip Address"
  93. document.getElementById("btnDoIt").disabled = ""
  94. Exit Sub
  95. End If
  96. End If 
  97.  
  98. Set perf_instance1 = wmi_service.get( sObjectPath )
  99.  
  100. N1 = perf_instance1.PercentProcessorTime
  101. D1 = perf_instance1.TimeStamp_Sys100NS
  102.  
  103. TimerID = window.setInterval("TehLoop", 1000)
  104. TimerWork = window.setInterval("StillWorking", 400)
  105.  
  106. If strIPAddress <> "." Then 
  107. document.getElementById("divTitle").innerText = "Waiting for " & strName & " on " & strIPAddress
  108. Else
  109. document.getElementById("divTitle").innerText = "Waiting for " & strName
  110. End If
  111.  
  112. document.getElementById("divMain").style.display = "none"
  113. document.getElementById("divStatus").style.display = ""
  114. document.title = "Waiting.."
  115. End Sub
  116.  
  117. Sub TehLoop
  118. Dim perf_instance2
  119.  
  120. Set perf_instance2 = wmi_service.get( sObjectPath )
  121. N2 = perf_instance2.PercentProcessorTime
  122. D2 = perf_instance2.TimeStamp_Sys100NS
  123.  
  124. 'Found This formula somewhere on the interwebs
  125. ' CounterType - PERF_100NSEC_TIMER
  126. ' Formula = ((N2 - N1) / (D2 - D1)) x 100
  127.  
  128. If ( 0 = (D2-D1) ) Then
  129. msgbox "process " & strName & " was not found"
  130. blnIsDone = true
  131. Else
  132. PercentProcessorTime = ((N2 - N1) / (D2 - D1)) * 100
  133.  
  134. document.getElementById("divUsage").innerText = "Usage: " & PercentProcessorTime
  135.  
  136. If PercentProcessorTime = 0 Then
  137. intWait = intWait + 1
  138. Progress 
  139. If intWait >= intTime Then
  140. blnIsDone = true
  141. blnIdle = true
  142. End If
  143. Else
  144. Progress
  145. intWait = 0
  146. End If
  147.  
  148. End If
  149.  
  150. N1 = N2
  151. D1 = D2
  152.  
  153. If blnIsDone Then
  154. btnCancel_OnClick
  155. End If
  156. End Sub
  157.  
  158. Sub Progress
  159. document.getElementById("divIdle").innerText = "Idle Seconds: " & intWait & " of " & intTime
  160.  
  161. If blnFocus Then
  162. document.title = "Waiting.."
  163. Else
  164. document.title = strName & " - " & intWait & " of " & intTime & " seconds"
  165. End If
  166.  
  167. End Sub
  168.  
  169. Sub StillWorking
  170.  
  171. Set div = document.getElementById("divWait")
  172.  
  173. If div.innerText <> "Waiting...." Then
  174. div.innerText = div.innerText & "."
  175. Else
  176. div.innerText = "Waiting."
  177. End If
  178.  
  179. End Sub
  180.  
  181. Sub btnCancel_OnClick
  182. Set wmi_service = nothing
  183.  
  184. If blnIdle Then
  185. document.title = strName & " is idle"
  186. Set objWC = window.document.getElementById("sndWC")
  187. If Not objWC Is Nothing Then
  188. objWC.src = "WorkCom.wav"
  189. End If
  190.  
  191. msgbox strName & " is idle"
  192. End If 
  193.  
  194. window.clearInterval(TimerID)
  195. window.clearInterval(TimerWork)
  196. document.getElementById("btnDoIt").disabled = ""
  197.  
  198. document.getElementById("divMain").style.display = ""
  199. document.getElementById("divStatus").style.display = "none"
  200. document.title = "Teh Waiter"
  201. End Sub
  202.  
  203. Sub FocusOut
  204. blnFocus = False
  205. End Sub
  206.  
  207. Sub FocusIn
  208. blnFocus = True
  209. End Sub
  210. -->
  211. </SCRIPT>
  212. </head>
  213. <body onfocusin="FocusIn" onfocusout="FocusOut" bgcolor="black">
  214. <form id="frmExecute">
  215. <center><font face="Comic Sans MS" color="blue">
  216. <bgsound src="" id="sndWC" />
  217. <div id="divMain">
  218. IP Address ("." for local):
  219. <br>
  220. <INPUT id="txtIP" tabIndex="1" type="text" maxLength="15" size="13" value="." name="txtIP" />
  221. <br>
  222. Process name (exp. "explorer"):
  223. <br>
  224. <INPUT id="txtName" tabIndex="2" type="text" size="13" value="explorer" name="txtName" />
  225. <br>
  226. Idle Time in seconds:
  227. <br>
  228. <INPUT id="txtTime" tabIndex="3" type="text" size="13" value="4" name="txtTime" />
  229. <br>
  230. <INPUT id="btnDoIt" tabIndex="4" type="button" value="Start" name="btnDoIt" />
  231. <br>
  232. </div>
  233.  
  234. <div id="divStatus" style="display: none">
  235. <INPUT id="btnCancel" tabIndex="4" type="button" value="Stop" name="btnCancel" />
  236. <br><br>
  237. <div id="divTitle"></div>
  238. <div id="divUsage">Usage: </div>
  239. <div id="divIdle">Idle Seconds: </div>
  240. <div id="divWait" style="color: red">Waiting...</div>
  241. </div>
  242. </font></center>
  243. </form>
  244. </body>
  245. </html>
Jan 3 '08 #1
1 7295
ShadowLocke
115 100+
Ah, theres an error when you copy and paste from this. On line 86 somehow got an extra space or two after "root"

it should be

Expand|Select|Wrap|Line Numbers
  1. Set wmi_service = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
with no spaces after "root"
Apr 7 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Phil Sherman | last post: by
4 posts views Thread by Brad Markisohn | last post: by
8 posts views Thread by Adrian | last post: by
11 posts views Thread by Michael Moreno | last post: by
12 posts views Thread by Perecli Manole | last post: by
2 posts views Thread by Reny | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.