stout, stderr to syslog via function with if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stout, stderr to syslog via function with if statement
# 8  
Old 09-06-2010
Code:
logger -p local1.notice -t mugsyback.debug TEST

please send /var/log/syslog
# 9  
Old 09-07-2010
Quote:
Originally Posted by john1212
Code:
logger -p local1.notice -t mugsyback.debug TEST

please send /var/log/syslog
Code:
$ logger -p local1.notice -t mugsyback.debug TEST
$ grep TEST /var/log/syslog
Sep  6 09:41:35 debvelopment mugsyback.debug: TEST

Logger itself works fine, including in the function.
# 10  
Old 09-07-2010
I think you want it.
Code:
cat /etc/rsyslog.d/mugsyback.conf 
# - mugsyback logging -
local1.notice                   /var/log/mugsyback.log
local9.debug			/dev/null

and
Code:
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                        else 
                                        logger -p local9.notice -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

This User Gave Thanks to john1212 For This Post:
# 11  
Old 09-07-2010
Nice work!

I simply hadn't thought of using syslog itself to send it to /dev/null.

That will work fine for my script.

Thankyou very much for taking the time to respond John1212, this is what I ended up using as a test:

Code:
$ cat /tmp/mugsyback.test
#!/bin/bash

logLevel=

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if test "${logLevel}" = "debug" ;
					then
                                        	logger -p local1.notice -t mugsyback.debug ${logMessage}
                                        else	
						logger -p local1.debug -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

mount -v /mnt/backup 2> >(sysLogger "critical") 1> >(sysLogger "debug") || { echo "failed" ; exit 1 ; }

And for syslog:

Code:
$ cat /etc/rsyslog.d/mugsyback.conf 
# - mugsyback logging -
# log all debug messages to /dev/null unless debugging enabled
# log everything else to /var/log/mugsyback.log
local1.notice	/var/log/mugsyback.log
local1.debug	/dev/null

I still get the file descriptor errors though so that's what I need to figure out next. Here is what the debug looks like now and shows at what stage the file descriptor errors occur (I haven't tidied up the logging yet)...

Code:
Sep  7 21:45:36 debvelopment mugsyback.info: Begin mugsyback at 1283859936
Sep  7 21:45:36 debvelopment mugsyback.debug: /dev/sdb1 on /mnt/backup type ext3 (rw)
Sep  7 21:45:36 debvelopment mugsyback.debug: check file found, continuing...
Sep  7 21:45:37 debvelopment mugsyback.debug: discovered 3 Backup Sources
Sep  7 21:45:37 debvelopment mugsyback.debug: "/mnt/backup/BACKUP/debvelopment" exists and contains folders
Sep  7 21:45:37 debvelopment mugsyback.debug: Removing /mnt/backup/BACKUP/debvelopment/2010-09-071283859469
Sep  7 21:45:37 debvelopment mugsyback.info: === BACKUP REPORT FOR "boot" ===
Sep  7 21:45:37 debvelopment mugsyback.info: Backup Engine = tar
Sep  7 21:45:48 debvelopment mugsyback.info: Backup Status: Success
Sep  7 21:45:49 debvelopment mugsyback.info: Number of files: 18
Sep  7 21:45:49 debvelopment mugsyback.info: Total file size: 106M bytes
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:45:50 debvelopment mugsyback.debug: "/mnt/root-snapshot" exists, not creating it...
Sep  7 21:45:50 debvelopment mugsyback.debug:   Logical volume "root-snapshot" created
Sep  7 21:45:50 debvelopment mugsyback.debug: mount: you didn't specify a filesystem type for /dev/mapper/vg00-root--snapshot
Sep  7 21:45:50 debvelopment mugsyback.debug:        I will try type ext3
Sep  7 21:45:50 debvelopment mugsyback.debug: /dev/mapper/vg00-root--snapshot on /mnt/root-snapshot type ext3 (rw)
Sep  7 21:45:50 debvelopment mugsyback.info: === BACKUP REPORT FOR "root" ===
Sep  7 21:45:50 debvelopment mugsyback.info: Backup Engine = tar
Sep  7 21:46:09 debvelopment mugsyback.info: Backup Status: Success
Sep  7 21:46:09 debvelopment mugsyback.info: Number of files: 2774
Sep  7 21:46:10 debvelopment mugsyback.info: Total file size: 4.8M bytes
Sep  7 21:46:10 debvelopment mugsyback.debug: /dev/mapper/vg00-root--snapshot umounted
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:46:11 debvelopment mugsyback.debug:   Logical volume "root-snapshot" successfully removed
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:46:11 debvelopment mugsyback.debug: "/mnt/var-data-snapshot" exists, not creating it...
Sep  7 21:46:11 debvelopment mugsyback.debug:   Logical volume "var-data-snapshot" created
Sep  7 21:46:12 debvelopment mugsyback.debug: mount: you didn't specify a filesystem type for /dev/mapper/vg00-var--data--snapshot
Sep  7 21:46:12 debvelopment mugsyback.debug:        I will try type xfs
Sep  7 21:46:12 debvelopment mugsyback.debug: /dev/mapper/vg00-var--data--snapshot on /mnt/var-data-snapshot type xfs (ro,nouuid)
Sep  7 21:46:12 debvelopment mugsyback.info: 
Sep  7 21:46:12 debvelopment mugsyback.info: Number of files: 5
Sep  7 21:46:12 debvelopment mugsyback.info: Number of files transferred: 0
Sep  7 21:46:12 debvelopment mugsyback.info: Total file size: 805.31M bytes
Sep  7 21:46:12 debvelopment mugsyback.info: Total transferred file size: 0 bytes
Sep  7 21:46:12 debvelopment mugsyback.info: Literal data: 0 bytes
Sep  7 21:46:12 debvelopment mugsyback.info: Matched data: 0 bytes
Sep  7 21:46:12 debvelopment mugsyback.info: File list size: 111
Sep  7 21:46:12 debvelopment mugsyback.info: File list generation time: 0.003 seconds
Sep  7 21:46:12 debvelopment mugsyback.info: File list transfer time: 0.000 seconds
Sep  7 21:46:12 debvelopment mugsyback.info: Total bytes sent: 126
Sep  7 21:46:12 debvelopment mugsyback.info: Total bytes received: 14
Sep  7 21:46:12 debvelopment mugsyback.info: 
Sep  7 21:46:12 debvelopment mugsyback.info: sent 126 bytes  received 14 bytes  280.00 bytes/sec
Sep  7 21:46:12 debvelopment mugsyback.info: total size is 805.31M  speedup is 5752188.34
Sep  7 21:46:12 debvelopment mugsyback.debug: /dev/mapper/vg00-var--data--snapshot umounted
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:46:13 debvelopment mugsyback.debug:   Logical volume "var-data-snapshot" successfully removed
Sep  7 21:46:13 debvelopment mugsyback.debug: /dev/sdb1 umounted
Sep  7 21:46:13 debvelopment mugsyback.info: === BACKUP MEDIA USAGE ===
Sep  7 21:46:13 debvelopment mugsyback.info: Filesystem Size Used Avail Use% Mounted on
Sep  7 21:46:13 debvelopment mugsyback.info: /dev/sdb1 8.5G 1.5G 6.7G 18% /mnt/backup
Sep  7 21:46:13 debvelopment mugsyback.debug: end mugsyback at 1283859973

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

A single statement without main function in c

A sample.c file is written with only one single statement. main; Segmentation fault occurred when executed that file. Any statement other than main; is written, for example unix; then it won't compile. why is this behaviour ! (2 Replies)
Discussion started by: techmonk
2 Replies

2. Shell Programming and Scripting

Ceil not working as function in awk statement

Hi, I have the following code in which i am trying to find ceil of 10th & 11th fields. For finding ceil i have a function in the awk statement. When i test it for some values say on command line it gives correct response(say $10=0 & $11=750). But when the same value occurs in a file having more 3... (5 Replies)
Discussion started by: siramitsharma
5 Replies

3. Shell Programming and Scripting

Running a function from a case statement

Hi, I have the below script that should take the command line option and run the desired script on another server. Only it doesn't seem to run the function, infact it just returns back to the command line. case $1 in 1) msgbacklog() ;; 2) jobstatus() ;; ... (10 Replies)
Discussion started by: chris01010
10 Replies

4. UNIX for Dummies Questions & Answers

Simplest way to format with If on stout?

So I'm trying to figure out a way to do some very simple formatting on standard output. I have a command that I will run (many many times) the output will either be true or false. So all i really want is to run the command and if its true write true in green and if its false to write false in red.... (10 Replies)
Discussion started by: MrEddy
10 Replies

5. Shell Programming and Scripting

Embed function in if statement

Hey everyone, I am just trying to figure out how to embed a function in an if statement. I have the following test script so far: PRIMARY=192.168.1.2 SECONDARY=192.168.1.1 function checkAlive { ping -c 1 -q $1 } if then echo "equaled 0" fi This... (1 Reply)
Discussion started by: msarro
1 Replies

6. Shell Programming and Scripting

Calling function in awk statement.

Hi All, I have an awk statement and a function defined in a script. I am trying to call the function from inside awk statement, i.e. awk ' myFunk () ;' filename But when I define myFunk() before awk, then I receive this error: s2.sh: line 48: syntax error: unexpected end of file and... (5 Replies)
Discussion started by: morningSunshine
5 Replies

7. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

8. Shell Programming and Scripting

Using Subroutine / Function in case statement

I have issue running functions under case statement #!/bin/bash single() { Commands } multiple() { Commands } until ; do echo -e " \t \t M A I N - M E N U Perforce delete script \n" (1 Reply)
Discussion started by: sriram003
1 Replies

9. UNIX for Dummies Questions & Answers

How can I find files by date or size from stout?

Hello all I wander if I make for example " ls -l " And it gives me all the files in the directory with the additional info like data size and privileges But what if I like to filter the stout result for example by date When I try to do: echo "`ls -l`" | grep "Jan 12" it gives me the... (2 Replies)
Discussion started by: umen
2 Replies

10. UNIX for Advanced & Expert Users

sending syslog output to stderr or stdout

Is there a way to send the syslog output for a given facility to stderr or stdout? I do not want to use the "tail" command to achieve this, I would like it to go directly to stderr. Thanks in advance (1 Reply)
Discussion started by: dmirza
1 Replies
Login or Register to Ask a Question