Daily diskspace alerts using OCS Inventory NG
By Mike on July 26th, 2007
Being proactive about managing system capacity is always a good idea. If you happen to use the OCS Inventory NG tool then here is a way for you to automatically have reports emailed to you (or various people) regarding systems with disk capacity below a threshold you set in a configuration file. This solution is geared towards Windows Servers, but you can easily change the select statement and use a different criteria (change where osname like ‘%server%’ to suit yourself). The results are sent as an attached CSV file, so you can easily view it in Excel or Open Office or even using vi if that is suits your fancy.
The percentage free space threshold, along with email addresses and domain names (aka workgroup names) are configured in a file named ocsdiff_conf.php. I run this (and other reports which I may post later) from a new subdirectory named reports that I created under /var/www/html/ocsreports. Be sure to set the same ownership and permissions on files in this directory as the other files under ocsreports. Here is what the file looks like:
> ocsdiff_conf.php
<?php
# ocsdiff configuration file
#
# Database host
$dbhost = "localhost";
$dbname = "ocsweb";
$dbuser = "ocs";
$dbpass = "XXX";
$adminemail = "admin@yourdomain.tld";
$diskthreshold = 5; # percentage free space threshold
$domains = array(
0 => array(
'name' => 'XXXXX',
'email' => "fred@yourdomain.tld,barney@yourdomain.tld"),
1 => array(
'name' => 'YYYYY',
'email' => "wilma@yourdomain.tld"),
2 => array(
'name' => 'ZZZZZ',
'email' => "betty@yourdomain.tld"));
$diskdomains = array(
0 => array(
'name' => 'XXXXX',
'email' => "fred@yourdomain.tld,barney@yourdomain.tld"),
1 => array(
'name' => 'YYYYY',
'email' => "wilma@yourdomain.tld"),
2 => array(
'name' => 'ZZZZZ',
'email' => "betty@yourdomain.tld"));
?>
The file is named like this because the first report I wrote was to generate reports on hardware changes (RAM and disk in particular). I know reports like this can be implemented using GLPI integration – but we already have a helpdesk/ticketing system and I just can’t justify the effort to setup GLPI when writing one’s own reports is just not that hard.
The report itself is contained in the code below, I named the file diskreport.php and I scheduled it to run everyday using cron. I run my cron jobs for OCS-NG reports under the apache account.
> diskreport.php
<?php
# This program will generate a daily report of disks with free space
# below a threshold set in the configuration file
# Written by Mike Seigafuse July 2007
#
# Get the configuration file
require_once("ocsdiff_conf.php");
# Initialize headers so that email will be readable as Excel attachment
$today = date("Y-m-d G:i:s");
$headers = "From: ITInventory@yourdomain.tld\\r\\n";
$headers .= "Reply-To: ITInventory@yourdomain.tld\\r\\n";
$headers .= "MIME-Version: 1.0\\r\\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\\r\\n";
$headers .= "Content-type: application/vnd.ms-excel\\r\\n";
$headers .= "Content-disposition: attachment; filename=$today.csv\\r\\n";
# Connect to database
$link = mysql_connect("$dbhost", "$dbuser", "$dbpass")
or die("Could not connect : " . mysql_error());
mysql_select_db("$dbname") or die("Could not select database");
# Now loop through the defined disk domains and prepare report
for($i = 0; $i < sizeof($diskdomains); $i++) {
$entry = $diskdomains[$i];
$name = $entry['name'];
$email = $entry['email'];
$subject = "Daily Disk Utilization Report for domain $name";
$msg = "Computer Name,Drive Letter,Type,Filesystem,Total,Free,Free %\\n";
# Now we have the domain name and contact, next get a current list of
# deviceIDs from this domain
$query = "select id, name, userid from hardware where workgroup='$name' and osname like '%server%'";
$deviceresults = mysql_query($query) or die("Query failed : " . mysql_error());
while ($line = mysql_fetch_array($deviceresults, MYSQL_ASSOC)) {
$deviceid = $line['id'];
$computername = rtrim($line['name']);
# Disk checking section
$query = "select letter, type, filesystem, total, free
from drives
where hardware_id='$deviceid' and
type = 'Hard Drive'";
$diskresults = mysql_query($query) or die("Query failed : " . mysql_error());
# All information is now collected, now generate output
while ($diskline = mysql_fetch_array($diskresults, MYSQL_ASSOC)) {
$driveletter = $diskline['letter'];
$drivetype = $diskline['type'];
$drivefs = $diskline['filesystem'];
$drivetotal = $diskline['total'];
$drivefree = $diskline['free'];
$freeperc = (($drivefree/$drivetotal) * 100);
if ($freeperc < $diskthreshold) {
$msg .= "$computername,$driveletter,$drivetype,$drivefs,$drivetotal,$drivefree,$freeperc\\n";
}
} # end of disk loop
} # end of devices in a domain loop
# send out the email for this domain
if ($msg == "") $msg = "No disk space issues found\\n";
mail("$email", $subject, $msg, $headers);
} # end of domain loop
# Almost done, just need to free up result memory
mysql_free_result($deviceresults);
mysql_free_result($diskresults);
# The End :)
?>
Be sure to change the From and Reply To addresses in at the top of the file to suit yourself. If I have time I will move this to the configuration file.
My version of OCS-NG displays 4020 on the screen, I believe this equates to V1.0 (I know 1.01 is released, but just haven’t had the time to upgrade yet). I am running on RHEL 4.
Use the link below to download a zip file of both of these files. If you have any comments or questions post a comment on this post and I will reply.

Leave a Response
SAManage is a leading provider of on-demand IT Asset Management services built using OCS-NG technology. Our service helps companies of all sizes effectively manage IT Assets, organize software licenses and detect risks and license compliance gaps. With no software or servers required, SAManage’s on-demand service can be easily deployed across multiple locations within minutes and provide visibility into complex IT infrastructures to ensure optimized IT asset utilization. For more information or to sign-up for a free 30-Days Trial, visit http://www.samanage.com or call 1-888-250-8971.
We are installing both in RHEL5 we got the alert but when we changing any hardware we didi not got alert can you help us.
Hi sarfraz,
What do you mean by changing hardware? Are you altering the hardware to simulate a low % of free disk space? Or were you expecting this utility to generate alerts on ahrdware changes?
I means, we are using this script for change management we can use it if not tel us and also we are getting alert but in attachment only we got this line
“Computer Name,Drive Letter,Type,Filesystem,Total,Free,Free %”
PLEASE HELP US.
Hi Mike!
i’m using ocsreport and i am search a tool that show me the changes in the machine with ocs-agent… this script(diskreport.php) show me the the disk-space but i don’t understand with put in the code! :S
can you help me?
Hi,
It was great to have a script to check disk space. It would be great if you could send me the OCSdiff script which sends mail of the difference to the hardware changes in the system.
Thanks in Advance.
Raman
[...] the OCS NG database. This script is implemented much like the daily disk reports from OCS (see this). Sorry there is no better documentation for this, I just have not had the time and soon will no [...]
It is now posted here – http://seigafuse.com/2008/12/19/ocs-diff-script/
in which folder on the ocsreports should I put these files?, or how do I use it?
dont worry, I just got the case closed. Thank you for these scripts.
Glad you figured it out Gabriel
Good job there are work on 1.3 too.
Hi All,
Does anyone have a script to send alerts whenever a new software is installed on any of computers which have OCS client installed?
Regards.
I suggest you take a look at GLPI, it has this capability when integrated with OCS-NG
Mike
Tks Mike.. I’ve already installed GLPI and integrated OCS plugin within it. However, I didn’t find much info regarding setting up this kind of alert on GLPI. If you know any source where I can get some how to, I’d really appreciate.
Regards;
How about a search of software where date is less than X. So if you wanted a listing of all new software installed in the last month use <1 in the date field of the search?
Not sure, I don't currently have access to running instances of either OCS-NG or GLPI, but this link gives me that idea…
You might also check out this plugin, it is light on documentation so you might just have to try it to see what it does.
Sorry I can’t be of more help, but as I said I don’t currently have access to these tools. Best of luck,
Mike
unable to run the script giving following error
./ocsdiff_conf.php: line 1: ?php: No such file or directory
./ocsdiff_conf.php: line 6: =: command not found
./ocsdiff_conf.php: line 7: =: command not found
./ocsdiff_conf.php: line 8: =: command not found
./ocsdiff_conf.php: line 9: =: command not found
./ocsdiff_conf.php: line 10: =: command not found
./ocsdiff_conf.php: line 11: =: command not found
./ocsdiff_conf.php: line 12: syntax error near unexpected token `(‘
./ocsdiff_conf.php: line 12: `$domains = array(‘
Why are you trying to run the config file, you would run the diskreport.php file which would load the configuration file. That said there is no reason the file should not run if you have the appropriate permissions set, have PHP installed, and call it correctly. Typically the report file is run from a cron job where PHP is called and the file name is passed to the PHP interpreter. If for some reason you want to run this form the command line you must use the command:
php -f filename.php
If that doesn’t help then please let me know how you are running the file and what version of PHP you are using.
[root@glpi reports]# php -f ocsdiff.php
PHP Parse error: syntax error, unexpected T_STRING in /var/www/html/ocsreports/reports/ocsdiff.php on line 14
[root@glpi reports]# php -f ocsdiff.php
PHP Notice: Undefined variable: curramresults in /var/www/html/ocsreports/reports/ocsdiff.php on line 112
PHP Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /var/www/html/ocsreports/reports/ocsdiff.php on line 112
PHP Notice: Undefined variable: snapramresults in /var/www/html/ocsreports/reports/ocsdiff.php on line 113
PHP Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /var/www/html/ocsreports/reports/ocsdiff.php on line 113
PHP Notice: Undefined variable: currdiskresults in /var/www/html/ocsreports/reports/ocsdiff.php on line 114
PHP Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /var/www/html/ocsreports/reports/ocsdiff.php on line 114
PHP Notice: Undefined variable: snapdiskresults in /var/www/html/ocsreports/reports/ocsdiff.php on line 115
PHP Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /var/www/html/ocsreports/reports/ocsdiff.php on line 115
Do you have both the config file and the diff script in the same directory with the appropriate permissions (read and execute)? In your first comment it says unexpected string on line 14… what do you have at line 14 in your script? Line 14 in my script is:
$headers .= “Reply-To: ITInventory@yourdomain.com“;
What does your line 14 look like?
Lets deal with the errors one at a time and start with the first before we move on.
pls provide the solution
I am trying to help you, please provide the information requested…
Here is what I get when I run it on my host:
[~]# php -f ocsdiff.php
[~]#
Here are what my permissions look like:
[~]# ls -al ocsdiff.php
-rw-r–r– 1 usermasked groupmasked 5433 Dec 19 16:06 ocsdiff.php
Did you do something like download to Windows, edit it there and then upload it to your Linux/UNIX host? If so you may want to download it directly to the UNIX host and edit it there. Windows has a nasty habit of converting line endings in UNIX (line feed) to CR and LF, so that could also be the root of your problem.
As noted, I am trying to help you but you also need to play a part in this …. Cura te ipsum
[root@glpi reports]# ls -al ocsdiff.php
-rwxrwxrwx 1 root root 5444 Dec 19 18:04 ocsdiff.php
I am downloading the file the windows and cpoy it to unix using winscp and than edit in linux itself.
Regards,
Ganesh Rane