Skip to content

Instantly share code, notes, and snippets.

@deepcoder
Created November 18, 2020 00:37
Show Gist options
  • Save deepcoder/b6da0d0ba69d04a5cd5025ffd5173c2a to your computer and use it in GitHub Desktop.
Save deepcoder/b6da0d0ba69d04a5cd5025ffd5173c2a to your computer and use it in GitHub Desktop.
language C : simple function to log message to remote syslog server via UDP port 514
// rsyslog_example.c
// 202011171619
//
// gcc -o rsyslog_example rsyslog_example.c
//
// simple function to log message to remote syslog server via UDP port 514
//
// why is this NOT part of the standard syslog library?????
//
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
// logging setup
// LOG_EMERG
// A panic condition was reported to all processes.
// LOG_ALERT
// A condition that should be corrected immediately.
// LOG_CRIT
// A critical condition.
// LOG_ERR
// An error message.
// LOG_WARNING
// A warning message.
// LOG_NOTICE
// A condition requiring special handling.
// LOG_INFO
// A general information message.
// LOG_DEBUG
// A message useful for debugging programs.
#define RSYSLOG_ADDRESS "192.168.yyy.xxx"
#define LOGMESSAGESIZE 512
char log_message[LOGMESSAGESIZE];
// this function sends a log message to a remote syslog server
// call with:
// log level
// hostname of logging server
// program name that is sending log message
// message
void send_remote_syslog_message(int log_level, char *hostname, char *program_name, char *message)
{
int sockfd, portno, n;
int serverlen;
int message_length;
struct sockaddr_in serveraddr;
struct hostent *server;
#define LOGBUFSIZE 1024
char buf[LOGBUFSIZE];
#define RSYSLOGPORT 514
// socket: create the socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
fprintf(stderr, "ERROR opening socket for remote syslog write");
exit(1);
}
// gethostbyname: get the server's DNS entry
server = gethostbyname(hostname);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host as %s for remote syslog write\n", hostname);
exit(1);
}
// build the server's Internet address
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(RSYSLOGPORT);
// build the syslog message
message_length = snprintf(buf, LOGBUFSIZE, "<%d>%s %s", LOG_USER + log_level, program_name, message);
// fprintf(stdout, "%s\n", buf);
// send the message to the server
serverlen = sizeof(serveraddr);
n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&serveraddr, serverlen);
if (n < 0)
{
fprintf(stderr,"ERROR in sendto for remote syslog write\n");
exit(1);
}
return;
}
int main(int argc, char **argv) {
setlogmask (LOG_UPTO (LOG_INFO));
openlog (__FILE__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
snprintf(log_message, LOGMESSAGESIZE, "%s v: %d.%d Starting.", __FILE__, VERSION_MAJOR, VERSION_MINOR);
send_remote_syslog_message(LOG_INFO, RSYSLOG_ADDRESS, __FILE__, log_message);
syslog (LOG_INFO, log_message);
//
//
//
//
snprintf(log_message, LOGMESSAGESIZE, "%s v: %d.%d <ctrl>-c signal received, exiting.", __FILE__, VERSION_MAJOR, VERSION_MINOR);
send_remote_syslog_message(LOG_INFO, RSYSLOG_ADDRESS, __FILE__, log_message);
syslog (LOG_INFO, log_message);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment