Browse Source

[syslog] Include hostname within syslog messages where possible

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
cbc54bf559
3 changed files with 57 additions and 8 deletions
  1. 3
    0
      src/include/ipxe/syslog.h
  2. 2
    4
      src/net/tcp/syslogs.c
  3. 52
    4
      src/net/udp/syslog.c

+ 3
- 0
src/include/ipxe/syslog.h View File

@@ -35,4 +35,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
35 35
 /** Syslog priority */
36 36
 #define SYSLOG_PRIORITY( facility, severity ) ( 8 * (facility) + (severity) )
37 37
 
38
+extern int syslog_send ( struct interface *xfer, unsigned int severity,
39
+			 const char *message, const char *terminator );
40
+
38 41
 #endif /* _IPXE_SYSLOG_H */

+ 2
- 4
src/net/tcp/syslogs.c View File

@@ -162,10 +162,8 @@ static void syslogs_putchar ( int character ) {
162 162
 	syslogs_entered = 1;
163 163
 
164 164
 	/* Send log message */
165
-	if ( ( rc = xfer_printf ( &syslogs, "<%d>ipxe: %s\n",
166
-				  SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
167
-						    syslogs_severity ),
168
-				  syslogs_buffer ) ) != 0 ) {
165
+	if ( ( rc = syslog_send ( &syslogs, syslogs_severity,
166
+				  syslogs_buffer, "\n" ) ) != 0 ) {
169 167
 		DBG ( "SYSLOGS could not send log message: %s\n",
170 168
 		      strerror ( rc ) );
171 169
 	}

+ 52
- 4
src/net/udp/syslog.c View File

@@ -25,6 +25,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
25 25
  */
26 26
 
27 27
 #include <stdint.h>
28
+#include <stdlib.h>
28 29
 #include <byteswap.h>
29 30
 #include <ipxe/xfer.h>
30 31
 #include <ipxe/open.h>
@@ -58,6 +59,41 @@ static struct interface_descriptor syslogger_desc =
58 59
 /** The syslog UDP interface */
59 60
 static struct interface syslogger = INTF_INIT ( syslogger_desc );
60 61
 
62
+/******************************************************************************
63
+ *
64
+ * Console driver
65
+ *
66
+ ******************************************************************************
67
+ */
68
+
69
+/** Host name (for log messages) */
70
+static char *syslog_hostname;
71
+
72
+/** Domain name (for log messages) */
73
+static char *syslog_domain;
74
+
75
+/**
76
+ * Transmit formatted syslog message
77
+ *
78
+ * @v xfer		Data transfer interface
79
+ * @v severity		Severity
80
+ * @v message		Message
81
+ * @v terminator	Message terminator
82
+ * @ret rc		Return status code
83
+ */
84
+int syslog_send ( struct interface *xfer, unsigned int severity,
85
+		  const char *message, const char *terminator ) {
86
+
87
+	return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
88
+			     SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
89
+					       severity ),
90
+			     ( syslog_hostname ? syslog_hostname : "" ),
91
+			     ( syslog_domain ? "." : "" ),
92
+			     ( syslog_domain ? syslog_domain : "" ),
93
+			     ( ( syslog_hostname || syslog_domain ) ? " " : ""),
94
+			     message, terminator );
95
+}
96
+
61 97
 /******************************************************************************
62 98
  *
63 99
  * Console driver
@@ -124,10 +160,8 @@ static void syslog_putchar ( int character ) {
124 160
 	syslog_entered = 1;
125 161
 
126 162
 	/* Send log message */
127
-	if ( ( rc = xfer_printf ( &syslogger, "<%d>ipxe: %s",
128
-				  SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
129
-						    syslog_severity ),
130
-				  syslog_buffer ) ) != 0 ) {
163
+	if ( ( rc = syslog_send ( &syslogger, syslog_severity,
164
+				  syslog_buffer, "" ) ) != 0 ) {
131 165
 		DBG ( "SYSLOG could not send log message: %s\n",
132 166
 		      strerror ( rc ) );
133 167
 	}
@@ -170,6 +204,20 @@ static int apply_syslog_settings ( void ) {
170 204
 	int len;
171 205
 	int rc;
172 206
 
207
+	/* Fetch hostname and domain name */
208
+	free ( syslog_hostname );
209
+	if ( ( len = fetch_string_setting_copy ( NULL, &hostname_setting,
210
+						 &syslog_hostname ) ) < 0 ) {
211
+		rc = len;
212
+		DBG ( "SYSLOG could not fetch hostname: %s\n", strerror ( rc ));
213
+	}
214
+	free ( syslog_domain );
215
+	if ( ( len = fetch_string_setting_copy ( NULL, &domain_setting,
216
+						 &syslog_domain ) ) < 0 ) {
217
+		rc = len;
218
+		DBG ( "SYSLOG could not fetch domain: %s\n", strerror ( rc ) );
219
+	}
220
+
173 221
 	/* Fetch log server */
174 222
 	syslog_console.disabled = 1;
175 223
 	old_addr.s_addr = sin_logserver->sin_addr.s_addr;

Loading…
Cancel
Save