I’ve done this a few times setting up MRTG and always get stuck when trying to use custom scripts.

The caveat is when you target a custom script the notation is:

Target[localhost.mem]: `/etc/mrtg/scripts/mem.pl`

or something along those lines, the caveat is that the quotes are not a ‘ but a ` ( the character at the top left of the keyboard)   this took me a little while to figure it out.

The perl script I am using is:

#!/usr/bin/perl

$machine = `/bin/hostname`;
chomp($machine);
$mem = `/usr/bin/free | grep Mem`;
$uptime = `/usr/bin/uptime`;

if ($mem =~ /^Mem:\s*(\d*)\s*(\d*)\s*(\d*)/) {
 $tot = $1;
 $used = $2;
 $free = $3;
}

if ($uptime =~ /up (.*),  \d* users?,/) {
 $up = $1;
}
print "$used\n";
print "$free\n";
print "$up\n";
print "$machine\n";

I took this from another site.. I will probablly write my shell scripts in PHP, they are just as easy , and powerful

Advertisements

Case : I need to loop over an array in PHP and remove elements based on some condition.

Solution:

print_r($arr);
foreach($arr as $key => $val)
{
  if($val == 'xyz')
  {
    unset($arr[$key]);
  }
}
print_r($arr);

Works great!

Recently had to install wordpress and Drupal on our shared server.

I wasn’t seeing any errors, just a status 500 Server Error.

So after poking around and trying some things. I had to make a .htaccess file with the following settings :

php_flag display_startup_errors on
php_flag display_errors on
php_flag register_globals off
php_value memory_limit 16M
php_value zend.ze1_compatibility_mode Off

This allowed me to see the errors and also to avoid a brutal compatibility bug our provider has setup in their version of PHP5 install. After these were set… no problems 🙂