SHARE
TWEET

VPS Site Monitor

bret_miller Oct 11th, 2011 281 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <package>
  2. <job id="WebSiteMonitor">
  3. <script language="jscript">
  4.         function getCurrentTime() {
  5.                 var dsDateObj = new Date();
  6.                 return(dsDateObj.getTime());
  7.         }
  8.         function getTimeDiff(t1,t2) {
  9.                 return(t2-t1);
  10.         }
  11. </script>
  12. <script language="vbscript">
  13.         option explicit
  14.         'on error resume next
  15.        
  16.         '********************************************************************************
  17.         ' This routine agressively checks a file every 5 minutes and restarts the server
  18.         ' if that file is not found. The idea is to keep the DreamHost VPS servers running
  19.         '********************************************************************************
  20.         ' Installation:
  21.         ' 1. Create a text file that contains just the string "123$SiteMonitorPSText!!~end"
  22.         '    with no CRLF at the end. Upload that to the root of a website on the VPS.
  23.         ' 2. Place this .wsf file on a Windows server or workstation.
  24.         ' 3. Fill in the values that follow these instructions.
  25.         ' 4. Uncomment out the 4 lines near the end if you reaaly want it to reboot your VPS.
  26.         ' 5. Schedule it to run every 5 minutes (or however often you want to check)
  27.        
  28.         dim sitestocheck,logfilename,mailto,mailfrom,mailrelay,mailport
  29.         ' sitestocheck contains an array of arrays that describe each site to check.
  30.         ' The fields are: description, test-file-url, value-in-test-file, reserved, API key, VPS name
  31.         sitestocheck = Array( _
  32.                 Array("example.com", _
  33.                         "http://example.com/SiteMonitorPS.txt", _
  34.                         Array("123$SiteMonitorPSText!!~end"), _
  35.                         "","API-KEY-HERE", _
  36.                         "ps99999") _
  37.         )
  38.         logfilename = "SiteMonitorPS.log"
  39.         mailto = "webmaster@example.com"
  40.         mailfrom = "postmaster@example.com"
  41.         mailrelay = "mail.example.com"
  42.         mailport = 25
  43.  
  44.         '********************************************************************************
  45.  
  46.         dim xmlhttp,stime,etime,rtime,statuscode,pagebody,pagelines,pageline
  47.         dim x,fso,myfile,myfilename,pageok,pagesok,msg,logfile
  48.         dim sitenum,sitetocheck,sitepageval, badsitelist(), badsites, badsite
  49.         dim waitfile,rebootlist(),reboots,msgsubj
  50.        
  51.         Set fso   = CreateObject("Scripting.FileSystemObject")
  52.         set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
  53.         call xmlhttp.setTimeouts(45000, 45000, 45000, 45000)
  54.         'call xmlhttp.setProxy(2,"10.4.1.2:3128")
  55.         set logfile = fso.OpenTextFile(logfilename,8,TRUE)
  56.         sitenum=0
  57.         badsites=0
  58.         reboots=0
  59.  
  60.         for each sitetocheck in sitestocheck
  61.                 sitenum=sitenum+1
  62.                 waitfile = "-PS-" & cstr(sitenum) & "-reboot-wait.txt"
  63.                 'First, load the login page so that variables are created and cookies set.
  64.                 stime = getCurrentTime()
  65.                 wscript.echo "Checking site " & sitetocheck(0)
  66.                 xmlhttp.open "GET",sitetocheck(1),true
  67.                 xmlhttp.send
  68.                 for x = 1 to 30*1000/100
  69.                         wscript.sleep 100
  70.                         if xmlhttp.readyState = 4 then exit for
  71.                 next
  72.                 if xmlhttp.readyState = 4 then
  73.                         statuscode = xmlhttp.status
  74.                         pagebody = xmlhttp.responseText
  75.                 else
  76.                         statuscode = "-1"
  77.                         pagebody = ""
  78.                 end if
  79.                 etime = getCurrentTime()
  80.                 rtime = cint((getTimeDiff(stime,etime)+5)/10)/100
  81.                 logline sitetocheck(0) & ": Took " & rtime & " seconds to retrieve page, status=" & statuscode
  82.                 'logline xmlhttp.getAllResponseHeaders
  83.                 pageok = false
  84.                 if statuscode = 200 then
  85.                         myfilename = "-PS-" & cstr(sitenum) & "-pagebody.htm"
  86.                         set myfile = fso.OpenTextFile(myfilename,2,TRUE)
  87.                         pagelines = split(pagebody,chr(10))
  88.                         for each pageline in pagelines
  89.                                 for each sitepageval in sitetocheck(2)
  90.                                         if instr(pageline,sitepageval) > 0 then pageok = true
  91.                                 next
  92.                                 'wscript.echo pageline
  93.                                 myfile.writeline pageline
  94.                         next
  95.                         myfile.close
  96.                 end if
  97.                 if pageok then
  98.                         logline sitetocheck(0) & ": Page validated OK."
  99.                         logfile.writeline now() & " " & sitetocheck(0) & ": site is functioning, response time = " & rtime & " seconds"
  100.                         if fso.fileExists(waitfile) then fso.deleteFile(waitfile)
  101.                 else
  102.                         redim preserve badsitelist(badsites)
  103.                         badsitelist(badsites)=sitetocheck(0)
  104.                         badsites=badsites+1
  105.                         logfile.writeline now() & " " & sitetocheck(0) & ": site is NOT functioning, response time = " & rtime & " seconds, status code=" & statuscode
  106.                         Call rebootsite(sitetocheck(4),sitetocheck(5))
  107.                 end if
  108.         next
  109.  
  110.         Set xmlhttp = nothing
  111.         logfile.close
  112.         set logfile = nothing
  113.        
  114.         if badsites > 0 then
  115.                 set msg = CreateObject("CDO.Message")
  116.                 msg.From = mailfrom
  117.                 msg.To = mailto
  118.                 msg.Subject = "**Downtime**"
  119.                 for each badsite in badsitelist
  120.                         msg.TextBody = msg.TextBody & "The " & badsite & " site is NOT currently functioning." & chr(10)
  121.                         msg.Subject = msg.Subject & " " & badsite
  122.                 next
  123.                 for each badsite in rebootlist
  124.                         msg.TextBody = msg.TextBody & "The " & badsite & " server was rebooted." & chr(10)
  125.                 next
  126.                 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  127.                 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailrelay
  128.                 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = mailport
  129.                 msg.Configuration.Fields.Update
  130.                 msg.Send
  131.                 Set msg = Nothing
  132.         end if
  133.  
  134.  
  135. function logline(myline)
  136.         wscript.echo myline
  137. end function
  138.  
  139. function rebootsite(myauth,myserver)
  140.         dim TypeLib,NewGUID,l,myfile,myfilename
  141.         if fso.fileExists(waitfile) then 'It takes twice down to issue a reboot command
  142.                 Call fso.deleteFile(waitfile) 'Make sure we clean it up so we don't repeatedly reboot
  143.                 wscript.echo "Trying to reboot: " & myserver
  144.                 redim preserve rebootlist(reboots)
  145.                 rebootlist(reboots)=myserver
  146.                 reboots=reboots+1
  147.                 Set TypeLib = CreateObject("Scriptlet.TypeLib")
  148.                 NewGUID = Trim(TypeLib.Guid)
  149.                 l = Len(NewGUID)
  150.                 'wscript.echo l & " - " & NewGUID
  151.                 NewGUID = mid(NewGUID,2,l-3)
  152.                 'wscript.echo l & " - " & NewGUID
  153.                 Set TypeLib = Nothing
  154.  
  155.                 xmlhttp.open "POST","https://panel.dreamhost.com/api/",false
  156.                 xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  157.                 xmlhttp.send "key="&myauth&"&cmd=dreamhost_ps-reboot&unique_id="&newGUID&"&ps="&myserver
  158.                 statuscode = xmlhttp.status
  159.                 wscript.echo xmlhttp.responseText
  160.                 myfilename = "-PS-" & cstr(sitenum) & "-reboot.txt"
  161.                 set myfile = fso.OpenTextFile(myfilename,2,TRUE)
  162.                 myfile.writeline "Status="&statuscode
  163.                 myfile.write xmlhttp.responseText
  164.                 myfile.close
  165.                 set myfile=nothing
  166.         else
  167.                 'comment out to disable rebooting-- comment 4 lines below to stop reboot feature
  168.                 'set myfile = fso.OpenTextFile(waitfile,2,TRUE)
  169.                 'myfile.writeline now() & " - waiting until next check to reboot " & myserver
  170.                 'myfile.close
  171.                 'set myfile=nothing
  172.         end if
  173. end function
  174. </script>
  175. </job>
  176. </package>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top