Add a system property, PROP_SYS_UPTIME, to place up-time markers
into rsyslog output.

Here is an example template, where this is used:

$template CoincidentFileFormat,"[UP=%$uptime%sec] %TIMESTAMP% %HOSTNAME% 
%syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"

and here is an example line of output using the above template:

[UP=20sec] Apr 12 21:50:00 atom kernel: imklog 5.8.7, log source = /proc/kmsg 
started.

Signed-off-by: Steven A. Falco <[email protected]>

---

I'm looking at optimizing boot time on an embedded system, and it is
useful to be able to display relative timestamps in addition to wall
clock timestamps.  To that end, I've worked up a patch that I am
using successfully for that purpose.

I'd like to contribute this to the rsyslog mainline, if you think
it is of general interest.

diff --git a/runtime/msg.c b/runtime/msg.c
index 8f92565..686e348 100644
--- a/runtime/msg.c
+++ b/runtime/msg.c
@@ -36,6 +36,7 @@
 #include <assert.h>
 #include <ctype.h>
 #include <sys/socket.h>
+#include <sys/sysinfo.h>
 #include <netdb.h>
 #include <libestr.h>
 #include <libee/libee.h>
@@ -562,6 +563,8 @@ propNameStrToID(uchar *pName, propid_t *pPropID)
                *pPropID = PROP_CEE;
        } else if(!strcmp((char*) pName, "$bom")) {
                *pPropID = PROP_SYS_BOM;
+       } else if(!strcmp((char*) pName, "$uptime")) {
+               *pPropID = PROP_SYS_UPTIME;
        } else {
                *pPropID = PROP_INVALID;
                iRet = RS_RET_VAR_NOT_FOUND;
@@ -2673,6 +2676,23 @@ uchar *MsgGetProp(msg_t *pMsg, struct templateEntry 
*pTpe,
                        pRes = (uchar*) "\xEF\xBB\xBF";
                        *pbMustBeFreed = 0;
                        break;
+               case PROP_SYS_UPTIME:
+                       {
+                               struct sysinfo s_info;
+
+                               if((pRes = (uchar*) MALLOC(sizeof(uchar) * 32)) 
== NULL) {
+                                       RET_OUT_OF_MEMORY;
+                               }
+                               *pbMustBeFreed = 1;
+
+                               if(sysinfo(&s_info) < 0) {
+                                       *pPropLen = sizeof("**SYSCALL 
FAILED**") - 1;
+                                       return(UCHAR_CONSTANT("**SYSCALL 
FAILED**"));
+                               }
+
+                               snprintf((char*) pRes, sizeof(uchar) * 32, 
"%ld", s_info.uptime);
+                       }
+                       break;
                default:
                        /* there is no point in continuing, we may even 
otherwise render the
                         * error message unreadable. rgerhards, 2007-07-10
diff --git a/runtime/rsyslog.h b/runtime/rsyslog.h
index ef43efd..b890788 100644
--- a/runtime/rsyslog.h
+++ b/runtime/rsyslog.h
@@ -141,6 +141,7 @@ typedef uintTiny    propid_t;
 #define PROP_CEE                       200
 #define PROP_CEE_ALL_JSON              201
 #define PROP_SYS_BOM                   159
+#define PROP_SYS_UPTIME                        160
 
 
 /* The error codes below are orginally "borrowed" from
_______________________________________________
rsyslog mailing list
http://lists.adiscon.net/mailman/listinfo/rsyslog
http://www.rsyslog.com/professional-services/
What's up with rsyslog? Follow https://twitter.com/rgerhards

Reply via email to