473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

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 7997
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

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

Similar topics

3
by: Phil Sherman | last post by:
What is the relationship between the snapshot elements: Log pages written Number write log IOs Number partial page log IOs There doesn't appear to be any information in my PDF System monitor...
4
by: Brad Markisohn | last post by:
I want to be notified when a PnP device is added or removed. To accomplish this task, I added two occurrences of a System.Management.ManagementEventWatcher. I've included a snippet of code that...
1
by: Shan Yong | last post by:
I am trying to monitor the CPU usage of a particualar process in VB.NET, and the best that I can come up with is get the TotalProcessor time using the System.Diagnostic.Process object. Is there...
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
11
by: Michael Moreno | last post by:
Hello, I have a C# server which has 4 worker threads running all the time. When I let the server runs for several hours, for some reasons the CPU usage of the application will shoot to 100% and...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
2
by: Reny | last post by:
Hi, I want to develop an internet usage monitor program in vb.net and monitor the time the user spend on internet etc. Can any one give me a clue on how to code it? Reny
0
by: mrpincer21 | last post by:
Could anyone help me by telling me how to create an excel document that monitors usage of many others by using visual basic program? I currently produce excel reports for my company but need to...
5
by: soulofstar | last post by:
I want to monitor the CPU and RAM usage using Python, Now, I know how to monitor RAM usage by Python from ctypes import * from ctypes.wintypes import * class MEMORYSTATUS(Structure): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.