Commit dbd086b0 authored by Guillaume Lucas's avatar Guillaume Lucas
Browse files

[Centreon-glpi] Commit initial. Version 1.0.1 en l'état.

parent 3fad3427
* * * * * apache @CENTREON_DIR@/cron/glpi-import >> @CENTREON_LOG@/centreon-glpi.log 2>&1
#!/usr/bin/php
<?php
/*
* Copyright 2005-2012 MERETHIS
* Centreon is developped by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation ; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <http://www.gnu.org/licenses>.
*
* Linking this program statically or dynamically with other modules is making a
* combined work based on this program. Thus, the terms and conditions of the GNU
* General Public License cover the whole combination.
*
* As a special exception, the copyright holders of this program give MERETHIS
* permission to link this program with independent modules to produce an executable,
* regardless of the license terms of these independent modules, and to copy and
* distribute the resulting executable under terms of MERETHIS choice, provided that
* MERETHIS also meet, for each linked independent module, the terms and conditions
* of the license of that module. An independent module is a module which is not
* derived from this program. If you modify this program, you may extend this
* exception to your version of the program, but you are not obliged to do so. If you
* do not wish to do so, delete this exception statement from your version.
*
* For more information : contact@centreon.com
*
* SVN : $URL$
* SVN : $Id$
*
*/
require_once "@CENTREON_ETC@/centreon.conf.php";
/**
* Output
*
* @param string $message
* @return void
*/
function output($message)
{
echo "[".date("d-m-Y H:i:s")."] ".$message."\n";
}
chdir($centreon_path."www/");
require_once $centreon_path."www/class/centreonDB.class.php";
require_once $centreon_path."www/modules/centreon-glpi/core/class/Centreon/Glpi/Client.php";
require_once $centreon_path."www/modules/centreon-glpi/core/class/Centreon/Glpi/Rule.php";
require_once $centreon_path."www/modules/centreon-glpi/core/class/Centreon/Glpi/Api.php";
require_once $centreon_path."www/modules/centreon-glpi/core/class/Centreon/Glpi/Utils.php";
try {
$db = new CentreonDB();
$ruleObj = new Centreon_Glpi_Rule($db);
$apiParams = Centreon_Glpi_Api::getParams($db);
$glpiClient = new Centreon_Glpi_Client(null, $apiParams);
$clapiCommandPrefix = $centreon_path."/www/modules/centreon-clapi/core/centreon -u ".$apiParams['clapi_login']." -p ".$apiParams['clapi_pass'];
exec($clapiCommandPrefix, $output, $clapiRes);
if ($clapiRes == 1) {
throw new Exception('Centreon CLAPI authentication failure');
}
/**
* Instances
*/
$res = $db->query("SELECT id, name FROM nagios_server");
$instances = array();
while ($row = $res->fetchRow()) {
$instances[$row['id']] = $row['name'];
}
/**
* Host groups
*/
$res = $db->query("SELECT hg_id, hg_name FROM hostgroup");
$hostgroups = array();
while ($row = $res->fetchRow()) {
$hostgroups[$row['hg_id']] = $row['hg_name'];
}
/**
* Host categories
*/
$res = $db->query("SELECT hc_id, hc_name FROM hostcategories");
$hostcategories = array();
while ($row = $res->fetchRow()) {
$hostcategories[$row['hc_id']] = $row['hc_name'];
}
/**
* Centreon host names and host templates
*/
$sql = "SELECT host_id, host_name, host_address, host_register FROM host";
$res = $db->query($sql);
$centreonHosts = array();
$hostTemplates = array();
while ($row = $res->fetchRow()) {
if ($row['host_register'] == 1) {
$centreonHosts[$row['host_name']] = $row['host_address'];
} elseif ($row['host_register'] == 0) {
$hostTemplates[$row['host_id']] = $row['host_name'];
}
}
/**
* Rules
*/
$sql = "SELECT r.rule_id, r.rule_name
FROM mod_glpi_matching m, mod_glpi_rules r
WHERE m.rule_id = r.rule_id
AND m.rule_dropdown_value != ''
GROUP BY rule_id
ORDER BY COUNT(r.rule_id) DESC";
$res = $db->query($sql);
$rules = array();
while ($row = $res->fetchRow()) {
$rules[$row['rule_id']] = $row['rule_name'];
}
$pollersToRestart = array();
foreach ($rules as $ruleId => $ruleName) {
$ruleInfo = $ruleObj->getSettings($ruleId);
$ipRange = $ruleInfo['ip_range'];
$hgs = array();
foreach ($ruleInfo['hostgroups'] as $hgId) {
$hgs[] = $hostgroups[$hgId];
}
$hcs = array();
foreach ($ruleInfo['hostcategories'] as $hcId) {
$hcs[] = $hostcategories[$hcId];
}
$sql = "SELECT rule_dropdown_name, rule_dropdown_value
FROM mod_glpi_matching
WHERE rule_id = ".$db->escape($ruleId)."
AND rule_dropdown_value != ''";
$res = $db->query($sql);
$filters = array();
while ($row = $res->fetchRow()) {
$filters[$row['rule_dropdown_name']] = preg_replace('/\*/', '%', $row['rule_dropdown_value']);
}
/**
* Network Equipment
*/
$networkFilter = $filters;
$networkFilter['itemtype'] = 'NetworkEquipment';
$matchedResult = $glpiClient->listObjects($networkFilter);
foreach ($matchedResult as $result) {
$hostInfo = $glpiClient->getNetworkEquipment(array('id' => $result['id']));
$address = $hostInfo['ip'];
if (!isset($centreonHosts[$result['name']])) {
$clapiCommand = $clapiCommandPrefix." -o HOST -a ADD -v \"".$result['name'].";".$result['name'].";".$address.";".$hostTemplates[$ruleInfo['host_template_id']].";".$instances[$ruleInfo['instance_id']].";".implode("|", $hgs)."\"";
exec($clapiCommand);
exec($clapiCommandPrefix." -o HOST -a APPLYTPL -v ".$result['name']);
if (count($hcs)) {
foreach ($hcs as $hcName) {
$clapiCommand = $clapiCommandPrefix." -o HC -a addmember -v \"".$hcName.";".$result['name']."\"";
exec($clapiCommand);
}
}
$centreonHosts[$result['name']] = $address;
output("Added ".$result['name']." - (Rule: ".$ruleName.")");
$pollersToRestart[$ruleInfo['instance_id']] = $instances[$ruleInfo['instance_id']];
} elseif ($centreonHosts[$result['name']] != $address) {
$clapiCommand = $clapiCommandPrefix." -o HOST -a SETPARAM -v \"".$result['name'].";address;".$address."\"";
exec($clapiCommand);
output("Updated IP address of ".$result['name']);
$pollersToRestart[$ruleInfo['instance_id']] = $instances[$ruleInfo['instance_id']];
}
}
/**
* Computers
* @todo refactor this
*/
$computerFilter = $filters;
$computerFilter['itemtype'] = 'Computer';
$matchedResult = $glpiClient->listObjects($computerFilter);
foreach ($matchedResult as $result) {
$hostInfo = $glpiClient->getNetworkports(array('id' => $result['id'],
'itemtype' => 'Computer'));
$address = "";
if (count($hostInfo)) {
foreach ($hostInfo as $info) {
if ($info['ip'] != "127.0.0.1") {
if (isset($ipRange) && $ipRange != "" && false == Centreon_Glpi_Utils::ipInRange($info['ip'], $ipRange)) {
continue;
} else {
$address = $info['ip'];
break;
}
}
}
}
if (!isset($centreonHosts[$result['name']]) && $address) {
$clapiCommand = $clapiCommandPrefix." -o HOST -a ADD -v \"".$result['name'].";".$result['name'].";".$address.";".$hostTemplates[$ruleInfo['host_template_id']].";".$instances[$ruleInfo['instance_id']].";".implode("|", $hgs)."\"";
exec($clapiCommand);
exec($clapiCommandPrefix." -o HOST -a APPLYTPL -v ".$result['name']);
if (count($hcs)) {
foreach ($hcs as $hcName) {
$clapiCommand = $clapiCommandPrefix." -o HC -a addmember -v \"".$hcName.";".$result['name']."\"";
exec($clapiCommand);
}
}
$centreonHosts[$result['name']] = $address;
output("Added ".$result['name']." - (Rule: ".$ruleName.")");
$pollersToRestart[$ruleInfo['instance_id']] = $instances[$ruleInfo['instance_id']];
} elseif ($centreonHosts[$result['name']] != $address) {
$clapiCommand = $clapiCommandPrefix." -o HOST -a SETPARAM -v \"".$result['name'].";address;".$address."\"";
exec($clapiCommand);
output("Updated IP address of ".$result['name']);
$pollersToRestart[$ruleInfo['instance_id']] = $instances[$ruleInfo['instance_id']];
}
}
}
if ($apiParams['clapi_restart'] == 1 && count($pollersToRestart)) {
foreach ($pollersToRestart as $pollerId => $pollerName) {
exec($clapiCommandPrefix." -a POLLERGENERATE -v $pollerId", $output, $returnVal);
if ($returnVal == 0) {
output("Generated files for poller ".$pollerName);
exec($clapiCommandPrefix." -a POLLERTEST -v $pollerId", $output, $returnVal);
if ($returnVal == 0) {
output("Configuration file checking successful for poller ".$pollerName);
exec($clapiCommandPrefix." -a CFGMOVE -v $pollerId", $output, $returnVal);
if ($returnVal == 0) {
output("Successfully moved configuration files to poller ".$pollerName);
exec($clapiCommandPrefix." -a POLLERRESTART -v $pollerId", $output, $returnVal);
if ($returnVal == 0) {
output("Poller ".$pollerName." successfully restarted");
} else {
output("Could not restart poller ".$pollerName);
}
} else {
output("Could not copy configuration files to poller ".$pollerName);
}
} else {
output("Engine configuration file checking was not successful for poller ".$pollerName);
}
} else {
output("Could not generate files for poller ".$pollerName);
}
}
}
} catch (Exception $e) {
output($e->getMessage());
}
?>
#!/bin/bash
#
# Copyright 2005-2012 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation ; either version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses>.
#
# Linking this program statically or dynamically with other modules is making a
# combined work based on this program. Thus, the terms and conditions of the GNU
# General Public License cover the whole combination.
#
# As a special exception, the copyright holders of this program give MERETHIS
# permission to link this program with independent modules to produce an executable,
# regardless of the license terms of these independent modules, and to copy and
# distribute the resulting executable under terms of MERETHIS choice, provided that
# MERETHIS also meet, for each linked independent module, the terms and conditions
# of the license of that module. An independent module is a module which is not
# derived from this program. If you modify this program, you may extend this
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
#
# For more information : contact@centreon.com
#
# SVN : $URL$
# SVN : $Id$
#
# List of files containing macros
CRON_MACRO_FILE="cron/glpi-import"
TEST_MACRO_FILE="www/modules/centreon-glpi/core/api_configuration/test.php"
SQL_MACRO_FILE="www/modules/centreon-glpi/sql/install.sql"
# Define Centreon GLPI version
NAME="centreon-glpi"
VERSION="1.0"
MODULE=$NAME.$VERSION
CRON_NAME="centreon-glpi"
CROND="/etc/cron.d"
LOG_VERSION="$MODULE installation"
FILE_CONF="instCentWeb.conf"
CENTREON_CONF="/etc/centreon/"
MODULE_DIR="www/modules/centreon-glpi/"
INSTALL_DIR_CENTREON="0"
WEB_USER="0"
WEB_GROUP="0"
NAGIOS_USER="0"
NAGIOS_GROUP="0"
NAGIOS_PLUGIN="0"
BACKUP="www/modules/centreon-glpi-backup"
PWD=`pwd`
TEMP=/tmp/install.$$
PROGRAM="install.sh"
DB_USER="0";
DB_HOST="0";
DB_PASSWORD="0";
DB_BASE="0";
DB_CENTSTORAGE="0";
PHP_INI="0";
#---
## {Print help and usage}
##
## @Stdout Usage and Help program
#----
function usage() {
local program=$PROGRAM
echo -e "Usage: $program -i"
exit 1
}
#---
## {Get Centreon install dir and user/group for apache}
#----
function get_centreon_parameters() {
INSTALL_DIR_CENTREON=`cat $CENTREON_CONF/$FILE_CONF | grep "INSTALL_DIR_CENTREON" | cut -d '=' -f2`;
LOG_DIR_CENTREON=`cat $CENTREON_CONF/$FILE_CONF | grep "CENTREON_LOG" | cut -d '=' -f2`;
WEB_USER=`cat $CENTREON_CONF/$FILE_CONF | grep "WEB_USER" | cut -d '=' -f2`;
WEB_GROUP=`cat $CENTREON_CONF/$FILE_CONF | grep "WEB_GROUP" | cut -d '=' -f2`;
NAGIOS_USER=`cat $CENTREON_CONF/$FILE_CONF | grep "NAGIOS_USER" | cut -d '=' -f2`;
NAGIOS_GROUP=`cat $CENTREON_CONF/$FILE_CONF | grep "NAGIOS_GROUP" | cut -d '=' -f2`;
NAGIOS_PLUGIN=`cat $CENTREON_CONF/$FILE_CONF | grep "NAGIOS_PLUGIN" | cut -d '=' -f2`;
NAGIOS_VAR=`cat $CENTREON_CONF/$FILE_CONF | grep "NAGIOS_VAR" | cut -d '=' -f2`;
CENTREON_VARLIB=`cat $CENTREON_CONF/$FILE_CONF | grep "CENTREON_VARLIB" | cut -d '=' -f2 | sed -e 's|\s||g'`;
CENTREON_ETC=`cat $CENTREON_CONF/$FILE_CONF | grep "CENTREON_ETC" | cut -d '=' -f2`;
if [ "$INSTALL_DIR_CENTREON" != "" ] && [ "$WEB_USER" != "" ] && [ "$WEB_GROUP" != "" ] ; then
return 1;
else
return 0;
fi
}
#---
## {Get location of instCentWeb.conf file}
##
## @Stdout Error message if user set incorrect directory
## @Stdin Path with must contain $FILE_CONF
#----
function get_centreon_configuration_location() {
echo ""
echo "$line"
echo -e "\tLoad parameters"
echo "$line"
err=1
while [ $err != 0 ]
do
echo -e "Please specify the directory that contains \"$FILE_CONF\""
echo -en "> "
read temp_read
if [ -z "$temp_read" ]; then
echo_failure "The directory does not exist!" "$fail"
fi
if [ -d $temp_read ] && [ -f $temp_read/$FILE_CONF ] ; then
err=0
CENTREON_CONF=$temp_read
else
echo_failure "File \"$FILE_CONF\" does not exist in this directory!" "$fail"
fi
done
}
#---
## {Install Centreon GLPI cron}
##
## @Stderr Log into $LOG_FILE
#----
function install_cron() {
echo ""
echo "$line"
echo -e "\tInstalling Centreon GLPI Cron"
echo "$line"
TEMP_D="/tmp/Install_Centreon_GLPI_Cron"
/bin/mkdir $TEMP_D >> $LOG_FILE 2>> $LOG_FILE
if [ -f /etc/cron.d/$CRON_NAME ] ; then
echo_success "Deleting old Centreon GLPI cron" "$ok"
/bin/rm -Rf "/etc/cron.d/$CRON_NAME"
fi
echo_success "Generating new Centreon GLPI cron" "$ok"
echo "* * * * * $WEB_USER $INSTALL_DIR_CENTREON/cron/glpi-import >> $LOG_DIR_CENTREON/centreon-glpi.log 2>&1" >> $TEMP_D/$CRON_NAME
echo_success "Copying cron" "$ok"
/bin/cp -Rf --preserve $TEMP_D/$CRON_NAME $CROND/$CRON_NAME >> $LOG_FILE 2>> $LOG_FILE
echo_success "Delete temp install directory" "$ok"
/bin/rm -Rf $TEMP_D >> $LOG_FILE 2>> $LOG_FILE
}
#---
## {Install Centreon GLPI Module}
##
## @Stderr Log into $LOG_FILE
#----
function install_module() {
echo ""
echo "$line"
echo -e "\tCentreon GLPI Module Installation"
echo "$line"
TEMP_D="/tmp/installation-glpi"
/bin/mkdir $TEMP_D >> $LOG_FILE 2>> $LOG_FILE
/bin/mkdir $TEMP_D/www >> $LOG_FILE 2>> $LOG_FILE
/bin/mkdir $TEMP_D/plugin >> $LOG_FILE 2>> $LOG_FILE
/bin/mkdir $TEMP_D/cron >> $LOG_FILE 2>> $LOG_FILE
/bin/cp -Rf www/* $TEMP_D/www >> $LOG_FILE 2>> $LOG_FILE
/bin/cp -Rf plugin/* $TEMP_D/plugin >> $LOG_FILE 2>> $LOG_FILE
/bin/cp -Rf cron/glpi-import $TEMP_D/cron >> $LOG_FILE 2>> $LOG_FILE
/bin/rm -Rf $TEMP_D/install $TEMP_D/*.log
echo_success "Replacing macros" "$ok"
replace_macro $TEMP_D/$CRON_MACRO_FILE
replace_macro $TEMP_D/$TEST_MACRO_FILE
replace_macro $TEMP_D/$SQL_MACRO_FILE
echo_success "Setting right" "$ok"
chmod -R 755 $TEMP_D/* >> $LOG_FILE 2>> $LOG_FILE
echo_success "Setting owner/group" "$ok"
/bin/chown -R $WEB_USER.$WEB_GROUP $TEMP_D/* >> $LOG_FILE 2>> $LOG_FILE
INSTALL_DIR_MODULE=$INSTALL_DIR_CENTREON/$MODULE_DIR
if [ -d $INSTALL_DIR_MODULE ] ; then
if [ -d $INSTALL_DIR_CENTREON/$BACKUP ] ; then
echo_success "Delete old Centreon GLPI backup" "$ok"
/bin/rm -Rf $INSTALL_DIR_CENTREON/$BACKUP/*
else
echo_success "Create a directory to backup old files" "$ok"
/bin/mkdir $INSTALL_DIR_CENTREON/$BACKUP
fi
echo_success "Backup old installation" "$ok"
mv $INSTALL_DIR_MODULE/* $INSTALL_DIR_CENTREON/$BACKUP >> $LOG_FILE 2>> $LOG_FILE
fi
if [ ! -d $INSTALL_DIR_MODULE ] ; then
echo_success "Create module directory" "$ok"
/bin/mkdir $INSTALL_DIR_MODULE >> $LOG_FILE 2>> $LOG_FILE
/bin/chown -R $WEB_USER.$WEB_GROUP $INSTALL_DIR_MODULE >> $LOG_FILE 2>> $LOG_FILE
/bin/chmod -R 755 $INSTALL_DIR_MODULE >> $LOG_FILE 2>> $LOG_FILE
fi
chmod +x $TEMP_D/plugin/glpi-ticket >> $LOG_FILE 2>> $LOG_FILE
dos2unix $TEMP_D/plugin/glpi-ticket >> $LOG_FILE 2>> $LOG_FILE
chown $NAGIOS_USER.$NAGIOS_GROUP $TEMP_D/plugin/glpi-ticket
chmod +x $TEMP_D/cron/glpi-import >> $LOG_FILE 2>> $LOG_FILE
dos2unix $TEMP_D/cron/glpi-import >> $LOG_FILE 2>> $LOG_FILE
chown $NAGIOS_USER.$NAGIOS_GROUP $TEMP_D/cron/glpi-import
echo_success "Copying module" "$ok"
/bin/cp -Rf --preserve $TEMP_D/www/* $INSTALL_DIR_CENTREON/www >> $LOG_FILE 2>> $LOG_FILE
echo_success "Copying cron" "$ok"
/bin/cp -Rf --preserve $TEMP_D/cron/* $INSTALL_DIR_CENTREON/cron/ >> $LOG_FILE 2>> $LOG_FILE
echo_success "Copying plugin" "$ok"
/bin/mkdir -p $NAGIOS_PLUGIN/glpi
chown -R $WEB_USER.$WEB_GROUP $NAGIOS_PLUGIN/glpi
/bin/cp -Rf --preserve $TEMP_D/plugin/* $NAGIOS_PLUGIN/ >> $LOG_FILE 2>> $LOG_FILE
echo_success "Delete temp install directory" "$ok"
/bin/rm -Rf $TEMP_D $TEMP >> $LOG_FILE 2>> $LOG_FILE
echo_success "\nThe $LOG_VERSION is finished" "$ok"
echo -e "See README and the log file for more details."
}
### Main
# define where is a centreon source
BASE_DIR=$(dirname $0)
## set directory
BASE_DIR=$( cd $BASE_DIR; pwd )
export BASE_DIR
if [ -z "${BASE_DIR#/}" ] ; then
echo -e "I think it is not right to have Centreon source on slash"
exit 1
fi
INSTALL_DIR="$BASE_DIR/install"
export INSTALL_DIR
# init variables
line="------------------------------------------------------------------------"
export line
## log default vars
. $INSTALL_DIR/vars
## load all functions used in this script
. $INSTALL_DIR/functions
## Define a default log file
LOG_FILE="$PWD/install.log"
## Valid if you are root
USERID=`id -u`
if [ "$USERID" != "0" ]; then
echo "You must exec with root user"
exit 1
fi
_tmp_install_opts="0"
silent_install="0"
user_conf=""
## Getopts
while getopts "iu:h" Options
do
case ${Options} in
i ) _tmp_install_opts="1"
silent_install="0"
;;
u ) _tmp_install_opts="1"
silent_install="1"
user_conf="${OPTARG%/}"
;;
\?|h) usage ;
exit 0
;;
* ) usage ;
exit 1
;;
esac
done
if [ "$_tmp_install_opts" -eq 0 ] ; then
usage
exit 1
fi
#Export variable for all programs
export silent_install user_install_vars CENTREON_CONF cinstall_opts inst_upgrade_dir
## init LOG_FILE
# backup old log file...
if [ -e "$LOG_FILE" ] ; then
mv "$LOG_FILE" "$LOG_FILE.`date +%Y%m%d-%H%M%S`"
fi
# Clean (and create) my log file
${CAT} << __EOL__ > "$LOG_FILE"
__EOL__
# Init GREP,CAT,SED,CHMOD,CHOWN variables
define_specific_binary_vars
${CAT} << __EOT__
###############################################################################
# #
# Module : Centreon GLPI version $VERSION #
# #
###############################################################################
__EOT__
## Test all binaries
BINARIES="rm cp mv ${CHMOD} ${CHOWN} echo more mkdir find ${GREP} ${CAT} ${SED}"
echo "$line"
echo -e "\tChecking all needed binaries"
echo "$line"
binary_fail="0"
# For the moment, I check if all binary exists in path.
# After, I must look a solution to use complet path by binary
for binary in $BINARIES; do
if [ ! -e ${binary} ] ; then
pathfind "$binary"
if [ "$?" -eq 0 ] ; then
echo_success "${binary}" "$ok"
else