]> gitweb.factorcode.org Git - factor.git/commitdiff
syslog: client for Syslog protocol.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Mar 2021 18:10:55 +0000 (11:10 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Mar 2021 18:10:55 +0000 (11:10 -0700)
extra/syslog/authors.txt [new file with mode: 0644]
extra/syslog/summary.txt [new file with mode: 0644]
extra/syslog/syslog.factor [new file with mode: 0644]

diff --git a/extra/syslog/authors.txt b/extra/syslog/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/syslog/summary.txt b/extra/syslog/summary.txt
new file mode 100644 (file)
index 0000000..8f53c50
--- /dev/null
@@ -0,0 +1 @@
+Syslog protocol
diff --git a/extra/syslog/syslog.factor b/extra/syslog/syslog.factor
new file mode 100644 (file)
index 0000000..548e9c7
--- /dev/null
@@ -0,0 +1,95 @@
+! Copyright (C) 2021 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors calendar calendar.format io io.encodings.utf8
+io.sockets io.streams.byte-array io.streams.string literals math
+math.parser namespaces sequences ;
+
+IN: syslog
+
+! RFC 3164 (http://www.faqs.org/rfcs/rfc3164.html)
+
+! The first part is called the PRI, the second part is the
+! HEADER, and the third part is the MSG.  The total length of
+! the packet MUST be 1024 bytes or less.  There is no minimum
+! length of the syslog message although sending a syslog packet
+! with no contents is worthless and SHOULD NOT be transmitted.
+
+CONSTANT: EMERGENCY  0 ! system is unusable
+CONSTANT: ALERT      1 ! action must be taken immediately
+CONSTANT: CRITICAL   2 ! critical conditions
+CONSTANT: ERROR      3 ! error conditions
+CONSTANT: WARNING    4 ! warning conditions
+CONSTANT: NOTICE     5 ! normal but significant condition
+CONSTANT: INFO       6 ! informational
+CONSTANT: DEBUG      7 ! debug-level messages
+
+CONSTANT: LOCAL0    16
+CONSTANT: LOCAL1    17
+CONSTANT: LOCAL2    18
+CONSTANT: LOCAL3    19
+CONSTANT: LOCAL4    20
+CONSTANT: LOCAL5    21
+CONSTANT: LOCAL6    22
+CONSTANT: LOCAL7    23
+
+SYMBOL: syslog-facility
+LOCAL0 syslog-facility set-global
+
+SYMBOL: syslog-server
+"127.0.0.1" 514 <inet4> syslog-server set-global
+
+<PRIVATE
+
+: dd ( timestamp -- )
+    day>> number>string 2 CHAR: \s pad-head write ;
+
+: write-priority ( level -- )
+    "<" write
+    syslog-facility get-global 8 * + number>string write
+    ">" write ;
+
+: write-mdhms ( timestamp -- )
+    { MONTH " " dd " " hh ":" mm ":" ss } formatted ;
+
+: write-timestamp ( -- )
+    now write-mdhms " " write ;
+
+: write-hostname ( -- )
+    host-name write " " write ;
+
+: write-syslog ( message level -- )
+    write-priority write-timestamp write-hostname write nl ;
+
+PRIVATE>
+
+: syslog ( message level -- )
+    utf8 [ write-syslog ] with-byte-writer
+    1024 short head
+    syslog-server get-global
+    $[ f 0 <inet4> <datagram> ]
+    send ;
+
+: log-debug ( message -- ) DEBUG syslog ;
+
+: log-info ( message -- ) INFO syslog ;
+
+: log-warning ( message -- ) WARNING syslog ;
+
+: log-error ( message -- ) ERROR syslog ;
+
+: log-critical ( message -- ) CRITICAL syslog ;
+
+: log-alert ( message -- ) ALERT syslog ;
+
+
+! Must contain only visible (printing) characters
+! "<PRI>TIMESTAMP HOSTNAME"
+
+! Example:
+! <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8
+
+! relays should handle cases:
+! 1) Valid PRI and TIMESTAMP
+! 2) Valid PRI but no TIMESTAMP or invalid TIMESTAMP
+! 3) No PRI or unidentifiable PRI