diff --git a/Scripts/Bash/Functions/Report/Pppd/pppd.sh b/Scripts/Bash/Functions/Report/Pppd/pppd.sh new file mode 100755 index 0000000..2b7c7f2 --- /dev/null +++ b/Scripts/Bash/Functions/Report/Pppd/pppd.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# +# pppd.sh -- This functionality parses /var/log/messages and returns +# time reports for pppd. +# +# Copyright (C) 2012 Alain Reguera Delgado +# +# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# ---------------------------------------------------------------------- +# $Id$ +# ---------------------------------------------------------------------- + +function pppd { + + local MESSAGES_PATH='' + local ENTRY='' + local ENTRY_ID='' + local ENTRIES='' + local ENTRIES_IDS='' + local INTERFACE="" + local INTERFACES_REGEX="" + + # Initizalice the flags used in this functionality. + local FLAG_INTERFACES='' + local FLAG_MONTH=$(LANG=C;date | gawk '{print $2}') + local FLAG_FIRSTDAY=$(LANG=C;date | gawk '{print $3}') + local FLAG_LASTDAY=${FLAG_FIRSTDAY} + + # Interpret arguments and options passed through command-line. + pppd_getOptions + + # Be sure the first day isn't greater than the last day used. In + # case it does, use the last day value as first day value. + # Otherwise no record would be output. + if [[ $FLAG_FIRSTDAY > $FLAG_LASTDAY ]];then + FLAG_FIRSTDAY=$FLAG_LASTDAY + fi + + # Define absolute path to log messages. + MESSAGES_PATH='/var/log/messages*' + + # Store log messages for later processing. Because messages + # required root privilages it is necessary to make a sudo action + # to read it. To avoid doing sudo actions constantly agains the + # same file, make just one sudo action to read the log messages + # and store the result in order for all other commands to be able + # read the information. Also, it is convenient to work with a + # unique source information instance from begining to end. + MESSAGES=$(sudo cat ${MESSAGES_PATH} \ + | gawk '{if ($5 ~ /^pppd\[[0-9]+]:$/ \ + && $1 == "'${FLAG_MONTH}'" \ + && $2 >= '${FLAG_FIRSTDAY}' \ + && $2 <= '${FLAG_LASTDAY}') print $0 }') + + # Be sure the interface isn't an empty value. When no interface + # value is passed as argument to this function, use `ppp0' + # interface as default value. + if [[ ! $FLAG_INTERFACES ]];then + FLAG_INTERFACES="ppp0" + fi + + # Be sure the interface meets the correct format. When a malformed + # interface is passed as first argument to this function, use + # `ppp0' interface as default value. + for INTERFACE in $FLAG_INTERFACES;do + if [[ $INTERFACE =~ '^ppp[0-9]$' ]];then + INTERFACE_REGEX="${INTERFACE_REGEX}${INTERFACE}|" + else + continue + fi + done + + # Build regular expression pattern to match interfaces passed as + # argument to pppd function. + INTERFACE_REGEX=$(echo $INTERFACE_REGEX | sed -r 's/\|$//') + INTERFACE_REGEX="(${INTERFACE_REGEX})" + + # Execute action names based on whether they were provided or not. + if [[ $ACTIONNAMS == '' ]];then + + # When action names are not provided, define action names that + # will take place, explicitly. This is the default behaviour. + return + + else + + # When action names are provided, loop through them and + # execute them one by one. + for ACTIONNAM in $ACTIONNAMS;do + + # Excecute action name. + ${CLI_SUBFUNC}_${ACTIONNAM} + + done + + fi + +} diff --git a/Scripts/Bash/Functions/Report/Pppd/pppd_getEntries.sh b/Scripts/Bash/Functions/Report/Pppd/pppd_getEntries.sh new file mode 100755 index 0000000..ed67c6a --- /dev/null +++ b/Scripts/Bash/Functions/Report/Pppd/pppd_getEntries.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# +# pppd_getEntries.sh -- This script returns entries generated by a +# succesful (on demand) pppd interaction for specific interfaces. +# +# Copyright (C) 2012 Alain Reguera Delgado +# +# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# ---------------------------------------------------------------------- +# $Id$ +# ---------------------------------------------------------------------- + +function pppd_getEntries { + + local PROCESS_ID='' + local PROCESS_IDS='' + local ENTRY='' + + # Build list of pppd's process ids. + PROCESS_IDS=$(pppd_getProcessIds) + + # Build list of entries related to pppd established connections. + for PROCESS_ID in $PROCESS_IDS;do + + # Increment entry counter to know where we are. + (( ENTRIES_CNT++ )) + + # Define an entry. + ENTRY=$(echo "$MESSAGES" \ + | gawk '{if ($5 ~ /pppd\['${PROCESS_ID}']:/) print $0}' ) + + # Exclude entries related to interfaces different from that + # one already specified. + echo "$ENTRY" | egrep "Using interface ${INTERFACE_REGEX}" > /dev/null + if [[ $? -eq 1 ]];then + continue + fi + + # Add entry to list of entries. + ENTRIES=$(echo "${ENTRY}"; echo "${ENTRIES}") + + done + + # Sort list of entries. + ENTRIES=$(echo "$ENTRIES" | sort ) + +} diff --git a/Scripts/Bash/Functions/Report/Pppd/pppd_getOptions.sh b/Scripts/Bash/Functions/Report/Pppd/pppd_getOptions.sh new file mode 100755 index 0000000..586c1d5 --- /dev/null +++ b/Scripts/Bash/Functions/Report/Pppd/pppd_getOptions.sh @@ -0,0 +1,92 @@ +#!/bin/bash +# +# pppd_getOptions.sh -- This function interpretes option arguments +# passed to `report' functionality through the command-line and +# defines action names accordingly. +# +# Copyright (C) 2012 Alain Reguera Delgado +# +# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# ---------------------------------------------------------------------- +# $Id$ +# ---------------------------------------------------------------------- + +function pppd_getOptions { + + + # Define short options we want to support. + local ARGSS="" + + # Define long options we want to support. + local ARGSL="interfaces:,connect-times,first-day:,last-day:" + + # Parse arguments using getopt(1) command parser. + cli_parseArguments + + # Reset positional parameters using output from (getopt) argument + # parser. + eval set -- "$ARGUMENTS" + + # Define action to take for each option passed. + while true; do + case "$1" in + + --quiet ) + FLAG_QUIET="true" + FLAG_DONT_COMMIT_CHANGES="true" + shift 1 + ;; + + --interfaces ) + FLAG_INTERFACES="$2" + shift 2 + ;; + + --last-day ) + FLAG_LASTDAY="$2" + shift 2 + ;; + + --first-day ) + FLAG_FIRSTDAY="$2" + shift 2 + ;; + + --connect-times ) + ACTIONNAMS="${ACTIONNAMS} outputConnectTimes" + shift 1 + ;; + + -- ) + # Remove the `--' argument from the list of arguments + # in order for processing non-option arguments + # correctly. At this point all option arguments have + # been processed already but the `--' argument still + # remains to mark ending of option arguments and + # begining of non-option arguments. The `--' argument + # needs to be removed here in order to avoid + # centos-art.sh script to process it as a path inside + # the repository, which obviously is not. + shift 1 + break + ;; + esac + done + + # Redefine ARGUMENTS variable using current positional parameters. + cli_parseArgumentsReDef "$@" + +} diff --git a/Scripts/Bash/Functions/Report/Pppd/pppd_getProcessIds.sh b/Scripts/Bash/Functions/Report/Pppd/pppd_getProcessIds.sh new file mode 100755 index 0000000..737bfa4 --- /dev/null +++ b/Scripts/Bash/Functions/Report/Pppd/pppd_getProcessIds.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# +# pppd_getProcessIds.sh --- This function parses the pppd's log +# messages and returns the process ids of each pppd connection. +# +# This function works with regular pppd log messages like the following: +# +# Jul 13 00:04:42 projects pppd[8076]: pppd 2.4.4 started by root, uid 0 +# Jul 13 00:04:42 projects pppd[8076]: Using interface ppp0 +# Jul 13 00:04:42 projects pppd[8076]: local IP address 10.64.64.64 +# Jul 13 00:04:42 projects pppd[8076]: remote IP address 10.112.112.112 +# Jul 13 00:04:58 projects pppd[8076]: Starting link +# Jul 13 00:05:34 projects pppd[8076]: Serial connection established. +# Jul 13 00:05:34 projects pppd[8076]: Connect: ppp0 <--> /dev/ttyACM0 +# Jul 13 00:05:36 projects pppd[8076]: PAP authentication succeeded +# Jul 13 00:05:36 projects pppd[8076]: Local IP address changed to 200.55.159.1 +# Jul 13 00:05:36 projects pppd[8076]: Remote IP address changed to 192.168.254.182 +# Jul 13 01:53:30 projects pppd[8076]: Terminating on signal 15 +# Jul 13 01:53:30 projects pppd[8076]: Connect time 107.9 minutes. +# Jul 13 01:53:30 projects pppd[8076]: Sent 2243056 bytes, received 21047698 bytes. +# Jul 13 01:53:36 projects pppd[8076]: Connection terminated. +# Jul 13 01:53:37 projects pppd[8076]: Terminating on signal 15 +# Jul 13 01:53:37 projects pppd[8076]: Exit. +# +# Copyright (C) 2012 Alain Reguera Delgado +# +# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# ---------------------------------------------------------------------- +# $Id$ +# ---------------------------------------------------------------------- + +function pppd_getProcessIds { + + echo "$MESSAGES" | gawk '{print $5}' \ + | sed -r 's!pppd\[([0-9]+)]:!\1!' | sort | uniq + +} diff --git a/Scripts/Bash/Functions/Report/Pppd/pppd_outputConnectTimes.sh b/Scripts/Bash/Functions/Report/Pppd/pppd_outputConnectTimes.sh new file mode 100755 index 0000000..a05539b --- /dev/null +++ b/Scripts/Bash/Functions/Report/Pppd/pppd_outputConnectTimes.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# +# pppd_outputConnectTime.sh -- Output connect time report for pppd +# interfaces. +# +# Copyright (C) 2012 Alain Reguera Delgado +# +# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# ---------------------------------------------------------------------- +# $Id$ +# ---------------------------------------------------------------------- + +function pppd_outputConnectTimes { + + local HOURS=0 + local MINUTES=0 + + # Build list of entries. This list contains the information will + # parse to build the connect time report. + pppd_getEntries + + # Define number of minutes. + MINUTES=$(echo "$ENTRIES" \ + | egrep 'Connect time [0-9.]+ minutes\.' \ + | cut -d' ' -f8 \ + | gawk '{ x += $1 }; END { print x }') + + # Define number of hours. + HOURS=$(echo "$MINUTES" | gawk '{print $1 / 60}') + + # Build connect time report. + cli_printMessage '-' --as-separator-line + cli_printMessage "`eval_gettext "Connect time through interfaces: \\\$INTERFACES"`" + cli_printMessage '-' --as-separator-line + echo "$ENTRIES" | egrep 'Connect time [0-9.]+ minutes.' + cli_printMessage '-' --as-separator-line + cli_printMessage "`eval_gettext "You have consumed \\\$MINUTES minutes (\\\${HOURS} hours)."`" + cli_printMessage '-' --as-separator-line + +} diff --git a/Scripts/Bash/Functions/Report/report.sh b/Scripts/Bash/Functions/Report/report.sh new file mode 100755 index 0000000..ca766be --- /dev/null +++ b/Scripts/Bash/Functions/Report/report.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# +# report.sh -- This functionality performs system reports. +# +# Copyright (C) 2012 Alain Reguera Delgado +# +# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +# +# ---------------------------------------------------------------------- +# $Id$ +# ---------------------------------------------------------------------- + +function report { + + local ACTIONNAM='' + local ACTIONNAMS='' + + # Define list of available sub-functionalities. + local CLI_SUBFUNC='' + local CLI_SUBFUNCS=$(cli_getFilesList --maxdepth 1 --mindepth 1 \ + --type d --pattern="^[[:alnum:]/]+$" ${CLI_FUNCDIR}/${CLI_FUNCDIRNAM}) + + # Show available sub-functionalities in order to choose one. + select CLI_SUBFUNC in $(basename $CLI_SUBFUNCS);do + break + done + + # Verify that no empty value be passed as sub-functionality. + if [[ $CLI_SUBFUNC == '' ]];then + exit + fi + + # Make sub-functionality all lower-case. This is required in order + # to build names correctly when exporting related function files. + CLI_SUBFUNC=$(cli_getRepoName $CLI_SUBFUNC -f) + + # Initialize specific functionalities. At this point we load all + # functionalities required into the application's execution + # environment and make them available, this way, to perform + # report-specific tasks. + cli_exportFunctions "${CLI_FUNCDIR}/${CLI_FUNCDIRNAM}/$(cli_getRepoName \ + ${CLI_SUBFUNC} -d)" "${CLI_SUBFUNC}" + + # Execute specific functionality. + ${CLI_SUBFUNC} + +}