Archive for April, 2009

Centreon and batch-adding hosts

Monday, April 27th, 2009

Centreon is a nice GUI wrapper for Nagios. It is using MySQL as its configuration engine, and it functions quite well. One thing Cacti can do but Centreon can’t is mass automatic addition of servers. I have had a new site with an installed Centreon, and I wanted to add about 40 servers to be monitored. This is a tedious work, and I was searching for some semi-automatic method of doing it.
This is not perfect, but it worked for me.
In this case I do not replicate service-group relationship, but only add a mass of servers.

First – create a text file containing a list of servers and IPs. It should look like this:
serv1:1.2.3.4
serv2:10.2.1.3
new_srv:2.3.4.1

I have placed in in /tmp/machines

Second – find the last host entry. In my case the DB name is Centreon, so I run the following command:

mysql -u root -p centreon -e’select host_id from host’

This should return a colum with numbers. Find the largest one and increment it by one. In my example the last one was 19, so my initial host_id will be 20.

You should now find the host_template_model_html_id you are to use. There are few methods for that, but the easiest way is to find another host information which matches to some level your desired information. In my case it was called “DB1”, so this looks like this:

mysql -u root -p centreon -e”select host_template_model_htm_id from host where host_name=’DB1′”

Please note that my blog formatting might change the quote character. You might not want to copy/paste it, but type it yourselves.

The result of the above query should give us a template ID. In my case it was “2”, which is fine by me.

If you want a better reference for the values entered, you can do a whole select for a single host to verify your values match mine:

mysql -u root -p centreon -e”select * from host where host_name=’DB1’G”

This should give you long listing and information of the host, as a reference.

My script goes like this, based on the assumptions made above:

#!/bin/bash
HOST=20
for i in `cat /tmp/machines`
do
   NAME=`echo $i | cut -f1 -d:`
   IP=`echo $2 | cut -f2 -d:`
   echo "insert into host values ('$HOST',2,NULL,NULL,1,NULL,NULL,NULL,NULL,'$NAME','$NAME','$IP',NULL,NULL,'2','2','2','2','2',NULL,'2',NULL,NULL,'2','2','2','2',NULL,NULL,'2',NULL,NULL,'0',NULL,'1','1');" >> /tmp/insert_sql.sql
   echo "insert into extended_host_information values('',$HOST,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);" >> /tmp/insert_sql.sql
   let HOST++
done

This should create a file called /tmp/insert_sql.sql which then should be first reviewed, and then inserted into your database.

Needless to say – back up your database first, just in case:

mysqldump -u root -p –opt -B centreon > /tmp/centreon_backup.sql

and then insert the newly created data:

mysql -u root -p centreon < /tmp/insert_sql.sql

Notice – at this point, no service relationship is created. I think it is quite a chore only to create the nodes. Adding the service relationships complicates things a bit, and I did not want to go there at this specific stage. However, for few tenths of monitored hosts, this is quite a lifesaver.

Notice that this is only Centreon configuration, and you will be required to apply it (through the GUI) to Nagios.

RedHat Cluster custom Oracle “Agent”/script V1.0

Friday, April 24th, 2009

Working with RH Cluster quite a lot, I have decided to create an online store of customer agents/scripts.

I have not, so far, invested the effort of making these agents accept settings from the cluster.conf file, but this might happen.

Let the library be!

Oracle DB script/agent:

Although I discovered (a bit late) that RH Cluster for Oracle Ent. Linux 5.2 does include oracle DB agent, this script should be good enough for RHEL4 RH Cluster versions as well.

This script only checks that the ‘smon’ process is up. Nothing fancy. This script can include, in the future, the ability to check that Oracle responses to SQL queries (meaning – actually working).

#!/bin/bash
#Service script for Oracle DB under RH Cluster
#Written by Ez-Aton
#http://run.tournament.org.il

# Global variables
ORACLE_USER=oracle
HOMEDIR=/home/$ORACLE_USER
OVERRIDE_FILE=/var/tmp/oracle_override
REC_LIST="user@domain.com"

function override () {
	if [ -f $OVERRIDE_FILE ]
	then
		exit 0
	fi
}

function start () {
	su - $ORACLE_USER -c ". $HOMEDIR/.bash_profile ; sqlplus / as sysdba << EOF
startup
EOF
"
	status
}

function stop () {
	su - $ORACLE_USER -c ". $HOMEDIR/.bash_profile ; sqlplus / as sysdba << EOF
shutdown immediate
EOF
"
	status && return 1 || return 0
}

function status () {
	ps -afu $ORACLE_USER | grep -v grep | grep smon
	return $?
}

function notify () {
	mail -s "$1 oracle on `hostname`" $REC_LIST < /dev/null
}

override
case "$1" in
start)	start
	notify $1
	;;
stop)	stop
#	notify $1
	;;
status)	status
	;;
*)	echo "Usage: $0 start|stop|status"
	;;
esac

I usually place this script (with execution permissions, of course) in /usr/local/sbin and call it as a “script” from the cluster configuration. You will probably be required to alter the first few variable lines to match to your environment.

Listener Agent/script:

The tnslsnr should be started/stopped as well, if we want the $ORACLE_HOME to migrate as well. This is its agent/script:

#!/bin/bash
#Service script for Oracle DB under RH Cluster
#Written by Ez-Aton
#http://run.tournament.org.il

ORACLE_USER=oracle
HOMEDIR=/home/$ORACLE_USER
OVERRIDE_FILE=/var/tmp/oracle_override

function override () {
if [ -f $OVERRIDE_FILE ]
then
exit 0
fi
}

function start () {
su - $ORACLE_USER -c ". $HOMEDIR/.bash_profile ; lsnrctl start"
status
}

function stop () {
su - $ORACLE_USER -c ". $HOMEDIR/.bash_profile ; lsnrctl stop"
status && return 1 || return 0
}

function status () {
su - $ORACLE_USER -c ". $HOMEDIR/.bash_profile ; lsnrctl status"
}

override
case "$1" in
start)    start
;;
stop)    stop
;;
status)    status
;;
*)    echo "Usage: $0 start|stop|status"
;;
esac

Again – place it in /usr/local/sbin and call it from the cluster configuration file as type “script”.

I will add more agents and more resources for RedHat Cluster in the future.

WordPress Mu 2.7.1 and Multi-Site Manager

Friday, April 24th, 2009

I have neglected this blog due to routine work lately. I have done some things, but not much to write home about, and being swamped with work, I just lacked the initiative to add new stuff, or to dwell into new technologies and problems. Routine is routine, and upgrade to WordPress Mu sometimes introduces new issues.

A very useful plugin called “Multi-Site Manager” is available for, well, managing easily multi-site configuration of a single WPMU from the GUI.

I have been using it for a while, and I was happy with it, however, after the latest Mu upgrade (2.7.1), a single problem appeared – you cannot manage sites from a newly created domain. Already existing domains functionality was not changed, but only new ones.

I through myself knee deep into its MySQL data, and got to the following workaround. This should be incorporated into the code, but me being such a lame PHP coder, maybe it’s not the best idea that I do it…

Problem:

You have created a new site, but from within its admin you cannot view the site admin panel. Manually entering the URL results in a “Permission Denied” error message.

Solution:

Login to your MySQL DB.

Use the MU database.

Find your new site ID:

select * from wp_site;

Write down the site number. Also, write down another site number – a small site would be best.

Get the correct entry for site_admins from some other blog

Select * from wp_sitemeta where meta_key=’site_admins’;

Response should look something like this:

+———+———+————-+——————————————+
| meta_id | site_id | meta_key | meta_value |
+———+———+————-+——————————————+
| 8 | 1 | site_admins | a:1:{i:0;s:5:”adm”;} |
| 467 | 21 | site_admins | a:1:{i:0;s:5:”adm”;} |
| 245 | 13 | site_admins | a:1:{i:0;s:5:”adm”;} |
| 45 | 3 | site_admins | a:1:{i:0;s:5:”adm”;} |
| 28 | 2 | site_admins | a:1:{i:0;s:5:”adm”;} |
| 62 | 5 | site_admins | a:1:{i:0;s:5:”adm”;} |
| 126 | 8 | site_admins | a:1:{i:0;s:5:”adm”;} |
+———+———+————-+——————————————+

Create a site_admin entry for your new site:

insert into wp_sitemeta (site_id,meta_key,meta_value) values (’22’,’site_admins’,’a:1:{i:0;s:5:”adm”;}’);

Im my example, 22 is the site ID, obtained from the first query.

Following that, refresh your dashboard GUI and you should be able to find your “Site Admin” panel there. From here you can define your desired settings for the site as you see fit.