SNMP Traphandle not working - linux

This is my first time working with SNMP, but after reading the SNMP pages I'm still having trouble getting a simple shell script to run when receiving a trap.
My /etc/snmp/snmptrapd.conf file looks like this:
# Example configuration file for snmptrapd
#
# No traps are handled by default, you must edit this file!
#
disableAuthorization yes
authCommunity log,execute,net public
# the generic traps
traphandle default /usr/local/bin/snmptrapd.sh
The snmptrapd.sh script just says "hello".
#!/bin/sh
echo "hello"
The script is executable and runs when executed independently:
> /usr/local/bin/snmptrapd.sh
hello
The snmptrapd is running as a background process:
> ps -ef | grep snmp
root 29477 1 0 14:49 ? 00:00:00 /usr/sbin/snmptrapd -Lsd -p /var/run/snmptrapd.pid -Cc /etc/snmp/snmptrapd.conf
And yet when I send a trap locally using snmptrap nothing happens:
> snmptrap -v 2c -c public localhost "" NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification netSnmpExampleHeartbeatRate i 123456
>
Now it seems that the trap does get logged, because the system log file (/var/log/messages) has the following entry:
Aug 8 15:46:10 <server_name> snmptrapd[29477]: 2017-08-08 15:46:10 localhost
[UDP: [127.0.0.1]:44928->[127.0.0.1]]:#012DISMAN-EVENT-MIB::sysUpTimeInstance =
Timeticks: (1338382434) 154 days, 21:43:44.34#011SNMPv2-MIB::snmpTrapOID.0 =
OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification#011NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate
= INTEGER: 123456
As far as I can see everything is set up correctly. If so, why is the trap handle not working and how can one check why the trap doesn't trigger the script?
Thanks in advance.
EDIT: When I added the -Ci option to the snmptrapd command line options I got the following error:
No log handling enabled - turning on stderr logging
: Unknown Object Identifier (Sub-id not found: (top) -> )

OK, so after looking around some more I found the answer.
The reason that we are not seeing the output is because snmptrapd is being run as a daemon and doesn't send its standard output to the console. One can replace this with
echo "hello" > $HOME/output.txt
and the word 'hello' appears in the output.txt file.
See also http://www.linuxquestions.org/questions/linux-newbie-8/net-snmp-trap-handling-4175420577/
and
https://superuser.com/questions/823435/where-to-log-stdout-and-stderr-of-a-daemon

Related

Trouble running a custom monit check, stderr maybe?

Trying to run this check from monit, but it doesn't work. The gravity program sends its output to stderr. Could it be that monit doesn't handle this properly because of the way it exec's the check?
contents of system.monitrc:
check program gravityStatus with path /usr/local/bin/check.sh
with timeout 10 seconds
if status !=0 then alert
check.sh:
root#tiki:~# cat /usr/local/bin/check.sh
#!/bin/bash
#This will return zero if all good
/usr/bin/gravity status |& /usr/bin/jq .SyncInfo.catching_up | grep -q 'false'
Output:
Program 'gravityStatus'
status Status failed
monitoring status Monitored
monitoring mode active
on reboot start
last exit value 1
last output parse error: Invalid numeric literal at line 1, column 6
data collected Tue, 01 Feb 2022 19:52:37
If I execute the contents of check.sh on the command line, the script works:
root#tiki:~# /usr/bin/gravity status |& /usr/bin/jq .SyncInfo.catching_up | grep -q 'false'
root#tiki:~# echo $?
0
I figured it out. I want to thank #boppy for his comment, it was very helpful. Here's what I did:
I changed the check.sh to just run 'gravity status' and then looked at the monit status. It says this:
Program 'gravityStatus'
status Status failed
monitoring status Monitored
monitoring mode active
on reboot start
last exit value 2
last output panic: $HOME is not defined
goroutine 1 [running]:
github.com/cosmos/cosmos-sdk/simapp.init.0()
/go/pkg/mod/github.com/cosmos/cosmos-sdk#v0.44.5/simapp/app.go:182 +0x189
The problem was that gravity status would die before it could send output to the jq process. gravity is a command that has to look at $HOME/.gravity were a bunch of configs are located. So the solution was to set $HOME to /root which is where all the gravity stuff is setup.

How do i fix bash error - /dev/tty No such device or address

As the question specifics ,i am getting this error while executing my bash script
In exact terms i get following error
bash: line 26: /dev/tty: No such device or address
bash: line 29: /dev/tty: No such device or address
Here are the concerned Line 26 and 29 in script respectively which causes the issue
read -e -p "Paste the links : " links </dev/tty
read -e -p "Enter your input : " sub </dev/tty
If someone wonders, i cannot simply remove writing to </dev/tty from line 26 and 29 , it causes different issues .. So basically i need fix or get alternative for writing to /dev/tty
I am executing my script by running -
curl raw_link | bash
Preferably i want a solution which only requires me to my edit my existing script .i don't want to run the script after saving it locally or execute it using any other way apart from curl raw_link | bash
ls -l /dev/tty returns the following
crw-rw-rw- 1 root root 5, 0 Aug 8 09:28 /dev/tty
ls -l </dev/tty returns the following
/bin/bash: /dev/tty: No such device or address
Also i would like to mention that this issue doesn't seem to be happening on every machine , i intend to use this script on Google Colab where i definitely do get this issue
To fix the bash error, you can try this workaround :
tty=$(readlink /proc/$$/fd/2)
read ... < $tty
$tty contains the actual tty device name.

bash redirect output to file but result is incomplete

The question of redirecting output of a command was already asked many times, however I am having a strange behavior. I am using a bash shell (debian) with version
4.3.30(1)-release and tried to redirect output to a file, however not everything are logged in the file.
The bin file that I tries to run is sauce-connectv4.4.1 for linux (client of saucelabs that is publicly available in internet)
If I run
#sudo ./bin/sc --doctor
it showed me a complete lines
it prints :
INFO: resolved to '23.42.27.27'
INFO: resolving 'g2.symcb.com' using
DNS server '10.0.0.5'...
(followed by other line)
INFO: 'google.com' is not in hosts file
INFO: URL https://google.com can be reached
However, if I redirect the same command to a file with the following command
#sudo ./bin/sc --doctor > alloutput.txt 2>&1
and do
#cat alloutput.txt
the same command output is logged, but deprecated as following:
INFO: resolved to '23.42.2me#mymachine:/opt/$
It has incomplete line, and the next lines that follows are not even logged (missing).
I have tried with >> for appending, it has the same problem. Using command &> alloutput.txt also is not printing the whole stuff. Can anyone point out how to get all lines of the above command to be logged completely to the text file?
UPDATE
In the end I manage to use the native binary logging by using --log
alloutput.txt where it completely provide me with the correct output.
However I let this issue open as I am still wondering why one misses some information/lines by doing an output redirection
you should try this: stdbuf -o0
like:
stdbuf -o0 ./bin/sc --doctor 2>&1 | tee -a alloutput.txt
That is a funny problem, I've never seen that happening before. I am going to go out on a limb here and suggest this, see how it works:
sudo ./bin/sc --doctor 2>&1 | tee -a alloutput.txt
#commandtorun &> alloutput.txt
This command will redirects both the error and output to same file.

Starting bash script

Hello I have such problem. I have init scripts, and I must run syslogd (busybox), so I have such code:
...
"$__start_program" $OPTIONS
....
If I
echo "$__start_program $OPTIONS"
it prints
/sbin/syslogd -s 512 -l 6 -L -O "/var/log/a.log"
I see this process in ps, but actually syslog don't start (there is no messages in log file about start and logger don't write something to log at all). But if I run this script manually from command line (with same arguments) it works fine. Can some one help me with this problem?
Don't use a string to store commands, that's not what they're for. The link provided in the comments contains some good discussion on the potential problems that this may cause.
It's not clear from the question where one string starts and the other ends but you should use a function to achieve what you are trying to do. Something like this:
log_daemon() {
param_s="$1"
logfile="$2"
/sbin/syslogd -s "$param_s" -l 6 -L -O "$logfile"
}
Then call it from your script like:
log_daemon 512 /var/log/a.log

Redirecting Output of Bash Child Scripts

I have a basic script that outputs various status messages. e.g.
~$ ./myscript.sh
0 of 100
1 of 100
2 of 100
...
I wanted to wrap this in a parent script, in order to run a sequence of child-scripts and send an email upon overall completion, e.g. topscript.sh
#!/bin/bash
START=$(date +%s)
/usr/local/bin/myscript.sh
/usr/local/bin/otherscript.sh
/usr/local/bin/anotherscript.sh
RET=$?
END=$(date +%s)
echo -e "Subject:Task Complete\nBegan on $START and finished at $END and exited with status $RET.\n" | sendmail -v group#mydomain.com
I'm running this like:
~$ topscript.sh >/var/log/topscript.log 2>&1
However, when I run tail -f /var/log/topscript.log to inspect the log I see nothing, even though running top shows myscript.sh is currently being executed, and therefore, presumably outputting status messages.
Why isn't the stdout/stderr from the child scripts being captured in the parent's log? How do I fix this?
EDIT: I'm also running these on a remote machine, connected via ssh using pseudo-tty allocation, e.g. ssh -t user#host. Could the pseudo-tty be interfering?
I just tried your the following: I have three files t1.sh, t2.sh, and t3.sh all with the following content:
#!/bin/bash
for((i=0;i<10;i++)) ; do
echo $i of 9
sleep 1
done
And a script called myscript.sh with the following content:
#!/bin/bash
./t1.sh
./t2.sh
./t3.sh
echo "All Done"
When I run ./myscript.sh > topscript.log 2>&1 and then in another terminal run tail -f topscript.log I see the lines being output just fine in the log file.
Perhaps the things being run in your subscripts use a large output buffer? I know when I've run python scripts before, it has a pretty big output buffer so you don't see any output for a while. Do you actually see the entire output in the email that gets sent out at the end of topscript.sh? Is it just that while the processes run you're not seeing the output?
try
unbuffer topscript.sh >/var/log/topscript.log 2>&1
Note that unbuffer is not always available as a std binary in old-style Unix platforms and may require a search and installation for a package to support it.
I hope this helps.

Resources