network-usage.sh 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/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.

22 23 24
# This script collects the machine network usage, generating and sending an XML
# to the server. Exit status:
#
25
# 0 - Success
26
# 1 - Error while loading configuration
27 28 29
# 2 - INEP not configured
# 3 - XML filename not defined
# 4 - Client not found or without execution permission
30
# 5 - Error while collecting network usage data
31
# 6 - Error while executing the client
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

# ------------------------------------------------------------------------------
# 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))"
68
source ${PREFIX}/load-config.sh || exit 1
69
PREVIOUS_TRAFFIC="${PREFIX}/net/previous"
70 71 72

exec >> ${LOGFILE} 2>&1

73 74
date +"%F %T - Start of network usage execution."

75 76 77 78 79
# 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."
80
    exit 2
81 82 83 84
fi

# Collect and send system data if elapsed enough time or if it's the first
# execution of the agent
85
if elapsedTime && test -d "${PREVIOUS_TRAFFIC}"; then
86 87
    if test -z "${NETXMLFILENAME}"; then
        printf "ERROR: Net XML filename not defined at configuration file.\n"
88
        exit 3
89 90 91 92 93 94 95
    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"
96
        exit 4
97 98 99 100 101
    fi

    # Collect system data
    if ! ${XMLPARSER} "${NETSCRIPTSDIR}" "${NETXMLFILENAME}"; then
        date +"%F %T - ERROR: while collecting network usage data."
102
        exit 5
103 104 105
    fi

    date +"%F %T - New network usage data collected."
106
    if ! clientExec --net-usage; then
107
        exit 6
108
    fi
109 110 111 112
	
	# Remove data already sent
	rm -rf ${PREVIOUS_TRAFFIC}
	
113 114 115 116
    # Record the current execution time to be used by elapsedTime()
    date +%s > ${NETLASTEXEC}
fi

117
date +"%F %T - Network usage terminated successfully."
118
exit 0