<div class="gmail_extra">You can use named pipes and a daemon that only acts when output from a given Syslog facility arrives.  It strikes a balance between the effort required to modify a driver and keep it up to date and spending extra resources on a daemon that does regular polling.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">I&#39;ve done similar for a client that wanted instantaneous e-mails when a given authorization error was sent to syslog.</div><div class="gmail_extra"><br></div>
<div class="gmail_extra">These are the instructions I gave for HP-UX but they should work in Linux with some small changes.</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra"><ol><li>
Create the named pipe &quot;mkfifo /var/adm/syslog/syslog.pipe </li><li>Put to following script in /usr/local/bin/syslogMailer or another location, modify the mail recipient and Pattern.</li></ol><div>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</div>
<div><div>#!/bin/sh</div><div><br></div><div># syslogMailer: a script to read stdin and turn each line into an alert</div><div># email typically this is used to read a named-pipe written to by syslog</div><div>#</div><div>
#   example usage: syslogMailer &lt; /etc/syslog.pipes/criticalMessages</div><div>#</div><div><br></div><div>alertRecipient=&quot;<a href="mailto:fireman@example.com">fireman@example.com</a>&quot;      # the mail recipient for alerts</div>
<div><br></div><div># process each line of input and produce an error message</div><div>while :</div><div>do</div><div>   while read line</div><div>   do</div><div>      # Send only messages I care about.</div><div>      echo ${line} | grep &quot;SOME_PATTERN&quot; &gt; /dev/null 2&gt;&amp;1</div>
<div>      if test $? -eq 0</div><div>        # send the alert</div><div>        echo &quot;${line}&quot; | mailx -s &quot;PATTERN alert on syslog&quot; ${alertRecipient}</div><div>      fi</div><div>   done</div><div>done</div>
</div><div><br></div><div>++++++++++++++++++++++++++++++++++++++++++++++++++++++</div><div><br></div><div>3. Put the following in /sbin/init.d/syslogMailer</div><div>+++++++++++++++++++++++++++++++++++++++++++++++++++</div>
<div><div>#!/sbin/sh</div><div>#</div><div># Start the syslogMailer script as a Daemon.</div><div>#</div><div><br></div><div>PATH=/sbin:/usr/sbin:/usr/bin:/usr/local/bin</div><div>export PATH</div><div><br></div><div>SM_PATH=/usr/local/bin/syslogMailer</div>
<div>NAMED_PIPE=/var/adm/syslog/syslog.pipe</div><div><br></div><div>rval=0</div><div><br></div><div>GetNamedProcIds()</div><div><br></div><div>{</div><div>        ps -e | awk -v mypid=$$ -v pname=$1        \</div><div>                        &#39;$NF==pname {                           \</div>
<div>                                if ($1 != mypid)                     \</div><div>                                        print $1                          \</div><div>                        }&#39;</div><div>}</div><div>
<br></div><div>case $1 in</div><div>start_msg)</div><div>        echo &quot;Starting syslogMailer&quot;</div><div>        ;;</div><div><br></div><div>&#39;start&#39;)</div><div><br></div><div>        pids=`GetNamedProcIds &quot;syslogMailer&quot;`</div>
<div>        if [ &quot;X$pids&quot; != &quot;X&quot; ]; then</div><div>            kill $pids 1&gt;/dev/null 2&gt;&amp;1</div><div>        fi</div><div><br></div><div>        $SM_PATH &lt; $NAMED_PIPE &gt; /dev/null 2&gt;&amp;1</div>
<div>        </div><div>        x=$?</div><div>        if [ $x -eq 0 ]</div><div>        then</div><div>                echo &#39;syslogMailer active&#39;</div><div>        else</div><div>                echo &quot;Warning: syslogMailer returned exit code: $x&quot;</div>
<div>                rval=1</div><div>        fi</div><div><br></div><div>        ;;</div><div>&#39;stop_msg&#39;)</div><div>        echo &quot;Stop syslogMailer daemon&quot;</div><div>        ;;</div><div><br></div><div>
&#39;stop&#39;)</div><div>        #</div><div>        # Determine PIDs of process(es) to stop</div><div>        #</div><div>        pids=`GetNamedProcIds &quot;syslogMailer&quot;`</div><div><br></div><div>        kill $pids 1&gt;/dev/null 2&gt;&amp;1</div>
<div>        echo &quot;syslogMailer stopped&quot;</div><div>        #</div><div>        # Set return value to be zero.</div><div>        #</div><div>        rval=0;</div><div><br></div><div>        ;;</div><div><br></div>
<div>*)</div><div>        echo &quot;usage: $0 {start}&quot;</div><div>        rval=1</div><div>        ;;</div><div>esac </div><div><br></div><div>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++</div><div><br></div>
<div>4. create links so the init.d script is called during startup and shutdown. &quot;ln -s /sbin/init.d/syslogMailer /sbin/rc2.d/S999syslogMailer;ln -s /sbin/init.d/syslogMailer /sbin/rc1.d/K999syslogMailer&quot;</div><div>
<br></div><div>  5. Edit /etc/syslog.conf and add the following line:</div><div><br></div><div>  facility.level               /var/adm/syslog/syslog.pipe</div><div><br></div><div>  6. Restart syslog &quot;/sbin/init.d/syslogd stop;/sbin/init.d/syslogd start&quot;</div>
<div><br></div><div>  7. Start syslogMailer &quot;/sbin/init.d/syslogMailer start&quot;</div><div><br></div><div>  8. Test by generating alert to syslog.</div></div><div><br></div><div><br></div></div><br><div class="gmail_quote">
On Mon, Apr 23, 2012 at 2:19 PM, Jim Ockers <span dir="ltr">&lt;<a href="mailto:ockers@ockers.net" target="_blank">ockers@ockers.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<u></u>



<div bgcolor="#ffffff" text="#000000">
<font size="+1"><tt>Hi everyone,<br>
<br>
The state of the art in determining ethernet link state, and taking any
action based on changes, seems to be to run a daemon that repeatedly
polls the network interface and using an ioctl (SIOCGMIIPHY,
SIOCETHTOOL) finds out if there is an ethernet link or not.<br>
<br>
It seems like it would be more deterministic, and less resource
intensive, to just have the network card device driver DO something (or
cause something to be done) when the link state changes.  For an
embedded device with limited CPU I think while(1) polling is
sub-optimal.  It also seems like it should not be too hard to get the
device driver to do something useful in addition to its printk()
message about the changed link state.<br>
<br>
How could I do this? I think system() is a bad idea because I&#39;m not
sure a device driver should be starting other processes that may block
or something.  Also system() requires unistd.h and the kernel
compile/build system doesn&#39;t seem to give drivers access to that sort
of normal libc functions.  Anyway I welcome any ideas.<br>
<br>
For example the driver doesn&#39;t compile with the 2 lines I added (marked
with +), and also unistd.h is not found even though I #included it:<br>
<br>
     if (!port-&gt;link) {<br>
         netif_carrier_off(dev);<br>
         printk(KERN_INFO &quot;%s: link down\n&quot;, dev-&gt;name);<br>
+        printk(KERN_INFO &quot;%s: link down, running
/usr/bin/wanlinkdown\n&quot;, dev-&gt;name);<br>
+        system(&quot;/usr/bin/wanlinkdown&quot;);<br>
         return;<br>
     }<br>
<br>
I&#39;m not heart-broken that this doesn&#39;t work because I didn&#39;t like this
approach to begin with, but I&#39;m still not sure exactly what would be
the best way to get a notification without polling.  So far the only
&quot;notification&quot; is the KERN_INFO message that gets printed to the dmesg
log and also whatever is specified in syslog.conf (<a href="http://kern.info" target="_blank">kern.info</a> goes to *,
which includes the console). In addition to the notification I want it
to run a program or something so I get explicit real-time notification
of link state changes without constantly polling.<br>
<br>
Thanks,<br>
Jim<span class="HOEnZb"><font color="#888888"><br>
</font></span></tt></font><span class="HOEnZb"><font color="#888888">
<pre cols="72">-- 
Jim Ockers, P.E., P.Eng. (<a href="mailto:ockers@ockers.net" target="_blank">ockers@ockers.net</a>)
Contact info: <a href="http://www.ockers.net/" target="_blank">http://www.ockers.net/</a>

</pre>
</font></span></div>

<br>_______________________________________________<br>
clue mailing list: <a href="mailto:clue@cluedenver.org">clue@cluedenver.org</a><br>
For information, account preferences, or to unsubscribe see:<br>
<a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br></blockquote></div><br></div>