Blame Scripts/Bash/Functions/Report/Pppd/pppd.sh

1b9e20
#!/bin/bash
1b9e20
#
1b9e20
# pppd.sh -- This functionality parses /var/log/messages and returns
1b9e20
# time reports for pppd.
1b9e20
#
1b9e20
# Copyright (C) 2012 Alain Reguera Delgado
1b9e20
#
1b9e20
# This program is free software; you can redistribute it and/or modify
1b9e20
# it under the terms of the GNU General Public License as published by
1b9e20
# the Free Software Foundation; either version 2 of the License, or
1b9e20
# (at your option) any later version.
1b9e20
#
1b9e20
# This program is distributed in the hope that it will be useful, but
1b9e20
# WITHOUT ANY WARRANTY; without even the implied warranty of
1b9e20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1b9e20
# General Public License for more details.
1b9e20
#
1b9e20
# You should have received a copy of the GNU General Public License
1b9e20
# along with this program; if not, write to the Free Software
1b9e20
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1b9e20
#
1b9e20
# ----------------------------------------------------------------------
1b9e20
# $Id$
1b9e20
# ----------------------------------------------------------------------
1b9e20
1b9e20
function pppd {
1b9e20
1b9e20
    local MESSAGES_PATH=''
1b9e20
    local ENTRY=''
1b9e20
    local ENTRY_ID=''
1b9e20
    local ENTRIES=''
1b9e20
    local ENTRIES_IDS=''
1b9e20
    local INTERFACE=""
1b9e20
    local INTERFACES_REGEX=""
1b9e20
1b9e20
    # Initizalice the flags used in this functionality.
1b9e20
    local FLAG_INTERFACES=''
1b9e20
    local FLAG_MONTH=$(LANG=C;date | gawk '{print $2}')
1b9e20
    local FLAG_FIRSTDAY=$(LANG=C;date | gawk '{print $3}')
1b9e20
    local FLAG_LASTDAY=${FLAG_FIRSTDAY}
1b9e20
1b9e20
    # Interpret arguments and options passed through command-line.
1b9e20
    pppd_getOptions
1b9e20
1b9e20
    # Be sure the first day isn't greater than the last day used. In
1b9e20
    # case it does, use the last day value as first day value.
1b9e20
    # Otherwise no record would be output.
1b9e20
    if [[ $FLAG_FIRSTDAY > $FLAG_LASTDAY ]];then
1b9e20
        FLAG_FIRSTDAY=$FLAG_LASTDAY
1b9e20
    fi
1b9e20
1b9e20
    # Define absolute path to log messages.
1b9e20
    MESSAGES_PATH='/var/log/messages*'
1b9e20
1b9e20
    # Store log messages for later processing. Because messages
1b9e20
    # required root privilages it is necessary to make a sudo action
1b9e20
    # to read it. To avoid doing sudo actions constantly agains the
1b9e20
    # same file, make just one sudo action to read the log messages
1b9e20
    # and store the result in order for all other commands to be able
1b9e20
    # read the information. Also, it is convenient to work with a
1b9e20
    # unique source information instance from begining to end.
1b9e20
    MESSAGES=$(sudo cat ${MESSAGES_PATH} \
1b9e20
        | gawk '{if ($5 ~ /^pppd\[[0-9]+]:$/ \
1b9e20
            && $1 == "'${FLAG_MONTH}'" \
1b9e20
            && $2 >= '${FLAG_FIRSTDAY}' \
1b9e20
            && $2 <= '${FLAG_LASTDAY}') print $0 }')
1b9e20
1b9e20
    # Be sure the interface isn't an empty value. When no interface
1b9e20
    # value is passed as argument to this function, use `ppp0'
1b9e20
    # interface as default value.
1b9e20
    if [[ ! $FLAG_INTERFACES ]];then
1b9e20
        FLAG_INTERFACES="ppp0"
1b9e20
    fi
1b9e20
1b9e20
    # Be sure the interface meets the correct format. When a malformed
1b9e20
    # interface is passed as first argument to this function, use
1b9e20
    # `ppp0' interface as default value.
1b9e20
    for INTERFACE in $FLAG_INTERFACES;do
1b9e20
        if [[ $INTERFACE =~ '^ppp[0-9]$' ]];then
1b9e20
            INTERFACE_REGEX="${INTERFACE_REGEX}${INTERFACE}|"
1b9e20
        else
1b9e20
            continue
1b9e20
        fi
1b9e20
    done
1b9e20
1b9e20
    # Build regular expression pattern to match interfaces passed as
1b9e20
    # argument to pppd function.
1b9e20
    INTERFACE_REGEX=$(echo $INTERFACE_REGEX | sed -r 's/\|$//')
1b9e20
    INTERFACE_REGEX="(${INTERFACE_REGEX})"
1b9e20
1b9e20
    # Execute action names based on whether they were provided or not.
1b9e20
    if [[ $ACTIONNAMS == '' ]];then
1b9e20
1b9e20
        # When action names are not provided, define action names that
1b9e20
        # will take place, explicitly. This is the default behaviour.
1b9e20
        return
1b9e20
1b9e20
    else
1b9e20
1b9e20
        # When action names are provided, loop through them and
1b9e20
        # execute them one by one.
1b9e20
        for ACTIONNAM in $ACTIONNAMS;do
1b9e20
1b9e20
            # Excecute action name.
1b9e20
            ${CLI_SUBFUNC}_${ACTIONNAM}
1b9e20
1b9e20
        done
1b9e20
1b9e20
    fi
1b9e20
1b9e20
}