WHAT'S NEW?
Loading...

Windows services ON/OFF?

Index

  1. Introduction
  2. The batch file
  3. Configuring Server Side Includes (SSI)
  4. Showing the services status
  5. References

1. Introduction

Last week, someone asked me to provide the status in a web site of some of the services running on the server. The site is a simple html document with links to other pages, but the idea was to show for each of those links, the service status which is running on the server. It's something you can't do with javascript, because as you know, JS runs on the client. I needed a solution, but I didn't want to redo the web in ASP or PHP...



To solve it, I created the following (different approaches will be recognized): first, I am going to create a batch file, which once is executed will create a "txt" file with the status of the services I need to verify. In the text file I'll save if the service is running or not. On the other hand, I need my website to check that file somehow and provide those statuses in the front-end. For that point, I used "Server Side Includes" aka SSI. This is a technology you can use in different platforms like IIS, PHP, ... which allows you to add some especial tags in your HTML and with that, you can include text files or even execute files. Something really handy believe me.


2. The batch file

This is a really easy batch file which checks the status of a particular service and saves it on a file. Notice the way we save text in the file the first time and the others. The first time we use just one ">", this way we replace the content of the text file. This way we assure there is no information from previous executions.

@echo OFF



sc query "MWMService" | FIND "STATE" | FIND "RUNNING" >nul

IF ERRORLEVEL 1 (echo|set /p=[["Master","OFF"],> statuses.txt ) 
ELSE (echo|set /p=[["Master","ON"],> statuses.txt)



sc query "MWMService CGG" | FIND "STATE" | FIND "RUNNING" >nul

IF ERRORLEVEL 1 (echo|set /p=["CGG","OFF"],>> statuses.txt ) 
ELSE (echo|set /p=["CGG","ON"],>> statuses.txt)



sc query "MWMService CGA" | FIND "STATE" | FIND "RUNNING" >nul

IF ERRORLEVEL 1 (echo|set /p=["CGA","OFF"],>> statuses.txt ) 
ELSE (echo|set /p=["CGA","ON"],>> statuses.txt)



sc query "MWMService CGH" | FIND "STATE" | FIND "RUNNING" >nul

IF ERRORLEVEL 1 (echo|set /p=["CGH","OFF"],>> statuses.txt ) 
ELSE (echo|set /p=["CGH","ON"],>> statuses.txt)



sc query "MWMService CGI" | FIND "STATE" | FIND "RUNNING" >nul

IF ERRORLEVEL 1 (echo|set /p=["AGI","OFF"]];>> statuses.txt ) 
ELSE (echo|set /p=["AGI","ON"]];>> statuses.txt)



The output of this bat file is:

[["Master","OFF"], ["CGG","ON"], ["CGA","OFF"], ["CGH","ON"], ["AGI","ON"]]; 

As you can see we have ON/OFF depending on the status of each of those services. The other important part is the format, how we get it, because it can be used directly as a javascript array. We will see in the following chapter how to do this.

3. Configuring Server Side Includes (SSI)

First, you probably will need to install this feature on your server. You can follow this link here, provided by the guys from iis.net, but no worries, the steps are really simple. In my case, I was using an Internet Information Server 8:


  • On the taskbar, click Start, point to Administrative Tools, and then click Server Manager. 
  • In the Server Manager hierarchy pane, expand Roles, and then click Web Server (IIS). 
  • In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services. 
  • On the Select Role Services page of the Add Role Services Wizard, select Server Side Includes, and then click Next. 
  • On the Confirm Installation Selections page, click Install. 
  • On the Results page, click Close.


Once the SSI is installed on your server, the next step it's just add it in your server mappings. So open your IIS instance and double click on "Handler Mappings", then click on "Add Module Mapping" and provide the following information:

Request Path: *.html
Module: ServerSideIncludeModule
Executable: blank
Name: SSI-html

4. Showing the services status

At this stage, we have everything prepared to start using SSI tags in our website. For example, you can use the following command inside of your html file to include the content of a file called test.txt



This way is really easy to save the content of that file in a javascript array and show the content wherever we want in our web page. See in this example how we use jQuery to replace some <div> tags in our web with the content of the file:

        $( document ).ready( function () {
            var items = 

            $("#Status1").replaceWith(items[0][1]);
            $("#Status2").replaceWith(items[1][1]);
            $("#Status3").replaceWith(items[2][1]);
            $("#Status4").replaceWith(items[3][1]);
            $("#Status5").replaceWith(items[4][1]);
        });

The array "items" will be populated previously by the server when the page is sent to the client. Once the page gets to the client, javascript will replace the content of our divs for the values in the array. This way we are accessing the windows services status directly.

I've been trying to find a way to run the bat file every time the page is loaded using the exec command from SSI but it has been impossible. Probably because of user rights. Anyway, as a walkaround. you can create a scheduled task running every minute to have the information updated.

I hope you enjoyed this post, please remember your opinion is really important for me to improve this blog. Looking forward to hear from you guys.

5. References

  • Activate SSI: http://www.iis.net/configreference/system.webserver/serversideinclude
  • Add mappings for SSI: http://tech.mikeal.com/blog1.php/server-side-includes-for-html-in-iis7


0 comments:

Post a Comment