#!/bin/bash # Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre # Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR # # This file is part of collect-agent # # collect-agent 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, or (at your option) any later version. # # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. # This script collects the machine network usage, generating and sending an XML # to the server. Exit status: # # 0 - Success # 1 - Error while loading configuration # 2 - INEP not configured # 3 - XML filename not defined # 4 - Client not found or without execution permission # 5 - Error while collecting network usage data # 6 - Error while executing the client # ------------------------------------------------------------------------------ # Function: elapsedTime # Verify if $DELAY seconds have elapsed since agent last execution. # Return true or false. function elapsedTime() { test -f "${NETLASTEXEC}" || return 0 atSeconds=$(date +%s) oldSeconds=$(cat ${NETLASTEXEC}) (( (${atSeconds}-${oldSeconds}) >= ${DELAY} )) return $? } # ------------------------------------------------------------------------------ # Function: clientExec # Execute client sender with all necessary parameters. # The parameters are obtained from the global variables. function clientExec() { date +"%F %T - Sending network usage data to the server." ${CLIENT} $* error=$? if test ${error} -ne 0; then date +"%F %T - ERROR: while sending network usage data to the server." return ${error} fi } # ------------------------------------------------------------------------------ export PREFIX="$(dirname $(readlink -f $0))" source ${PREFIX}/load-config.sh || exit 1 PREVIOUS_TRAFFIC="${PREFIX}/net/previous" exec >> ${LOGFILE} 2>&1 date +"%F %T - Start of network usage execution." # Check if inep file exists and is configured if ! test -f ${CLIENTCONFDIR}/inep -a -n "$(cat ${CLIENTCONFDIR}/inep \ 2> /dev/null)"; then date \ +"%F %T - INEP not found in ${CLIENTCONFDIR}/inep, waiting configuration." exit 2 fi # Collect and send system data if elapsed enough time or if it's the first # execution of the agent if elapsedTime && test -d "${PREVIOUS_TRAFFIC}"; then if test -z "${NETXMLFILENAME}"; then printf "ERROR: Net XML filename not defined at configuration file.\n" exit 3 else # Create "data" directory if it does not exist mkdir -p $(dirname $NETXMLFILENAME) fi if test ! -x "${CLIENT}"; then printf "ERROR: Client not found or without execution permission.\n" printf "${SCRIPTPATH} finished.\n" exit 4 fi # Collect system data if ! ${XMLPARSER} "${NETSCRIPTSDIR}" "${NETXMLFILENAME}"; then date +"%F %T - ERROR: while collecting network usage data." exit 5 fi date +"%F %T - New network usage data collected." if ! clientExec --net-usage; then exit 6 fi # Remove data already sent rm -rf ${PREVIOUS_TRAFFIC} # Record the current execution time to be used by elapsedTime() date +%s > ${NETLASTEXEC} fi date +"%F %T - Network usage terminated successfully." exit 0