Plugins:Uptime

From OCS Inventory NG
Jump to: navigation, search

Retrieve Machine Uptime

Windows Agent configuration

Create the script

This script is a VB script. You have to place it in C:\Program Files\OCS Inventory Agent\Plugins . The name is not important. Nothing more to do.

Code

'' Plugin for OCS Inventory NG 2.x
'' Uptime 1.0.1 (04/05/2014)
'' PRIOU Guillaume

Set objWMIService=GetObject("winmgmts:\\.\root\cimv2")
Set colOperatingSystems=objWMIService.ExecQuery _
  ("Select * From Win32_PerfFormattedData_PerfOS_System")
For Each objOS in colOperatingSystems
  intSystemUptime=Int(objOS.SystemUpTime)
  TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)
  Wscript.echo "<UPTIME>"
  Wscript.echo UpTime(intSystemUptime)
  Wscript.echo "</UPTIME>"
next
Function UpTime(S)
  M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
  UpTime="<TIME>" & D &" Days, " & H MOD 24 & " Hours, " & M & " Minutes </TIME>"
End Function

Check the result

Result is inserted in XML file generated by agent. If you want to store this file in local, launch your agent with option /XML="C:\Documents and Settings\All Users\Application Data\OCS Inventory NG\Agent".

<UPTIME>
      <TIME>0 Days, 6 Hours, 53 Minutes</TIME>
</UPTIME>

Database server

Create table in database to store informations

You have to create a new table which will receive new data

CREATE TABLE IF NOT EXISTS `uptime` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT,
  `HARDWARE_ID` INT(11) NOT NULL,
  `TIME` VARCHAR(64) DEFAULT NULL,
  PRIMARY KEY  (`ID`,`HARDWARE_ID`)
) ENGINE=INNODB ;

Management server

Modifiy the engine

Warning: Since OCS inventory NG 2.1, you do not have to modify Map.pm directly, but you have to configure engine to include perl scripts


In OCS Inventory NG Server configuration file (z-ocsinventory-server.conf), define Plugins conf and script path :

===== PLUGINS =====
  PerlSetEnv OCS_PLUGINS_PERL_DIR "/etc/ocsinventory-server/perl"
  PerlSetEnv OCS_PLUGINS_CONF_DIR "/etc/ocsinventory-server/plugins"

Create /etc/ocsinventory-server/plugins/Uptime.conf

PerlModule Apache::Ocsinventory::Plugins::Uptime::Map

Create /etc/ocsinventory-server/perl/Apache/Ocsinventory/Plugins/Uptime/Map.pm

###############################################################################
## OCSINVENTORY-NG
## Copyleft Guillaume PROTET 2013
## Web : http://www.ocsinventory-ng.org
##
## This code is open source and may be copied and modified as long as the source
## code is always made freely available.
## Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
################################################################################

package Apache::Ocsinventory::Plugins::Uptime::Map;

use strict;

use Apache::Ocsinventory::Map;
#Plugin UPTIME
$DATA_MAP{uptime} = {
	mask => 0,
	multi => 1,
	auto => 1,
	delOnReplace => 1,
	sortBy => 'TIME',
	writeDiff => 0,
	cache => 0,
	fields => {
             TIME => {},
}
};
1;
Warning: When you will upgrade your OCS Inventory Server, Map.pm will be overwrite. Don't forget to save this file before upgrading.


Communication server

Create the workspace

First, it's important to respect files architecture. It's necessary to create a new branch in the

plugins/computer_detail directory

and named your new directory

cd_<your_module_name> 

In our case, cd_uptime. This directory will contain cd_uptime.php.


Then, import your 3 icons in

plugins/computer_detail/img

The name of the new icon will be in the form ms_<your_module_name>.png for inactive icon and ms_<your_module_name>_a.png for active icon. Add an icon is not an obligation because you can add your module to a menu already in place or even create a menu on a icon.


Here, icons you can use :

Cd uptime.pngCd uptime a.pngCd uptime d.png

Create the PHP file

In our case, we have just to display a table with 2 colomns and 1 line. So I will use an existant page : plugins/computer_detail/cd_videos/cd_video.php. I copy it in

plugins/computer_detail/cd_uptime/

and rename it in

cd_uptime.php

I modify the code simply to display what I want

<?php
//====================================================================================
// OCS INVENTORY REPORTS
// Copyleft Erwan GOALOU 2010 (erwan(at)ocsinventory-ng(pt)org)
// Web: http://www.ocsinventory-ng.org
//
// This code is open source and may be copied and modified as long as the source
// code is always made freely available.
// Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
//====================================================================================
 
	print_item_header("UPTIME");
	if (!isset($protectedPost['SHOW']))
		$protectedPost['SHOW'] = 'NOSHOW';
	$form_name="uptime";
	$table_name=$form_name;
	echo "<form name='".$form_name."' id='".$form_name."' method='POST' action=''>";
	$list_fields=array('<center>UPTIME</center>' => 'TIME',);
	if($show_all_column)
		$list_col_cant_del=$list_fields;
	else
		$list_col_cant_del=array('TIME' => 'TIME');
 
	$default_fields= $list_fields;
	//$tab_options['FILTRE']=array('NAME'=>$l->g(212),'REGVALUE'=>$l->g(213));;
	$queryDetails  = "SELECT * FROM uptime WHERE (hardware_id = $systemid)";
	tab_req($table_name,$list_fields,$default_fields,$list_col_cant_del,$queryDetails,$form_name,80,$tab_options);
	echo "</form>";
?>
Warning: You need to fix good permissions on cd_uptime.php.


chmod 0755 ./cd_uptime.php
chown root:www-data ./cd_uptime.php

Activate the plugin

You have to activate your plugin in

/plugins/computer_detail/cd_config.txt

Here, just the diff of this file. Mobile icon is never used, so I desactivate it to replace by my "SecurityCenter" icon

<ORDER>
.......
.......
21:cd_uptime
</ORDER>

<LBL>
.......
.......
cd_uptime:cd_uptime
</LBL>

<ISAVAIL>
.......
.......
cd_uptime:uptime
</ISAVAIL>

<URL>
.......
cd_uptime:21
.......
</URL>

Display the result in administration console

Connect to the GUI, select a machine, and click on new icon. You can see the uptime of the host.

Plugin uptime 3.png