Archive for the ‘ Scripting ’ Category

Recently we are having problems with our site not loading properly and found that the culprit is that the new version of php-memcache (php-memcache-5.2.9) is not compatible with the memcached daemon installed on the server. This is a simple script to test if memcached is working or not. A brief background about memcached.

From php.net

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

From danga.com

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

The Script:

<?php

$memcached = new Memcache;
$memcached->connect(’localhost’, 11211) or die (”Could not connect”);

$memver = $memcached->getVersion();
echo “Server’s Version: “.$memver.”\n”;

$tmp_obj = new stdClass;
$tmp_obj->str_attr = ‘test memcached and php-memcache module’;
$tmp_obj->int_attr = 123456789;

$memcached->set(’key’, $tmp_obj, false, 10) or die (”Failed to save data at the server”);
echo “Store data in the cache (data will expire in 10 seconds)\n”;

$result = $memcached->get(’key’);
echo “Data from the memcached:\n”;

var_dump($result);

?>

The output:

raxso@raxso:-$ php get-memcache-info.php
Server’s Version: 1.2.2
Store data in the cache (data will expire in 10 seconds)
Data from the memcached:
object(stdClass)#3 (2) {
["str_attr"]=>
string(38) “test memcached and php-memcache module”
["int_attr"]=>
int(123456789)
}

Happy Scripting… :)

Like this blog? Why not buy me a cup of coffee?

This is a small bash script i got from Full Circle Magazine Issue 21, with some modifications to properly format the output for Red Hat Linux variants.

#!/bin/bash
clear
echo
#Create a colored header for the section
echo -e “\E[33mLINUX"
echo -e "\E[33m-----------------------------------------------------------------"
#Display info about the kernel, distro and uptime, cut commands cut out only the applicable info from output
echo -e "\E[34mKernel:  `uname -r`"
echo "Distro:   `cat /etc/issue| head -1 `"
echo "Uptime:   `uptime | awk '{print $1}'`"
echo
#Create another colored header for the CPU section
echo -e "\E[0mCPU"
echo -e "\E[0m-----------------------------------------------------------------"
#Create a variable to store the name of the CPU model (since it's a dual core, it will be the same name for both cores.  awk cuts out the name of the CPU,
#and the head -n 1 removes the unnecessary duplicates (will work for a quad core too) and the sed command removes the leading blank space before the line.
#the s/^[ \t]*// means substitute any leading whitespace (be it a space or a tab) and replace it with nothing.
ModelName=`awk ‘{FS = “:”} /^model name/ {print $2} ‘ /proc/cpuinfo|head -n 1|sed ’s/^[ \t]*//’`
echo -e “\E[33mCPU 1:           $ModelName"
echo "Speed of CPU 1:   `grep "cpu MHz" /proc/cpuinfo | cut --delimiter=":" -f 2|head -n 1|sed 's/^[ \t]*//’` mhz”
echo “CPU 2:            $ModelName”
echo “Speed of CPU 2:   `grep “cpu MHz” /proc/cpuinfo | cut –delimiter=”:” -f 2|tail -n 1|sed ’s/^[ \t]*//’` mhz”
#Below, the indent command is used to remove any indentation (setting the indentation level to 0 with the -i0 flag).  However, indent isn’t installed by
#default, anyone who would like could try to replace it with sed (think of it as a challenge).
echo
echo -e “\E[31mMEMORY”
echo -e “\E[31m—————————————————————–”
memtot=`grep “MemTotal” /proc/meminfo | cut -c 12-22 |tail -n 1`
memfr=`grep “MemFree” /proc/meminfo | cut -c 12-22 |  tail -n 1`
memcache=`grep “Cached” /proc/meminfo | cut -c 12-22| tail -n 1`
memtotal=$(($memtot/1024))
memfree=$(($memfr/1024))
memcache=$(($memcache/1024))
memfreetotal=$(($memfree+$memcache))
memused=$(($memtotal-$memfreetotal))
echo -e “\E[32mMemory Used:     $memused MB”
echo “Memory Free:     $memfreetotal MB”
echo “Memory Total:    $memtotal MB”
echo
tput sgr0 #set colors back to normal in the terminal
#create variables with the various theme names, if you need to remove any just comment the variable out and the echo line pertaining to that variable.
exit 0 # Exit the program after running

And this is the output..

Linux Details

You can modify the script and add some specific details, like if you wanted to add some monitoring like load average.

Happy Scripting…..

Like this blog? Why not buy me a cup of coffee?

Here’s a simple shell script that will check the available disk space on the root filesystem and send an e-mail message if the used space is at 90% or more.

#!/bin/bash
# monitor available disk space
SPACE=`df | sed -n ‘/\/$/p’ | gawk ‘{print $4}’ | sed ’s/%//’`
if [ $SPACE -ge 90 ]
then
echo “Disk space on root at $SPACE% used” | mail -s “Disk warning” email@example.com
fi

you can set the shell script to execute at a set number of times to monitor the disk activity. You can do this using the cron. For a low-volume file server, you may only have to run the script once a day:

30 0 * * * /usr/local/scripts/diskmon

For a high-volume file server environment, you may have to monitor this every 30mins.

/30 * * * * /usr/local/scripts/diskmon

Like this blog? Why not buy me a cup of coffee?

This is a small script i use to backup MySQl Database, I use it in my everyday system administration job so feel free to use and configure it. This will backup a database and zip the file.

#!/bin/sh

date=`date -I`
mysqldump -uroot -ptest123 database | gzip -a > /home/mysql-backup/database-$date.sql.gz

Here is another one if you want to backup all database and zip the file.

#!/bin/sh

date=`date -I`
mysqldump -uroot -ptest123 –all-databases | gzip -a > /home/mysql-backup/database-$date.sql.gz

Like this blog? Why not buy me a cup of coffee?