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.

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.