Initial commit of files
This commit is contained in:
22
daemons/logd/CMakeLists.txt
Normal file
22
daemons/logd/CMakeLists.txt
Normal file
@ -0,0 +1,22 @@
|
||||
cmake_minimum_required(VERSION 3.7)
|
||||
set(NAME logd)
|
||||
project(Cnomicon-Daemon)
|
||||
set(TARGET ${NAME})
|
||||
set(CMAKE_INSTALL_PREFIX ..)
|
||||
|
||||
# includes
|
||||
include_directories(
|
||||
/usr/include/libxml2
|
||||
../include/libnet++ ../include)
|
||||
link_directories(../lib)
|
||||
link_libraries(net++ net pthread rt)
|
||||
|
||||
# sources
|
||||
set(SRCS
|
||||
LogDaemon.cpp
|
||||
)
|
||||
|
||||
# executables
|
||||
add_executable(${TARGET} ${TARGET}.cpp ${SRCS})
|
||||
|
||||
install(TARGETS ${TARGET} DESTINATION sbin)
|
||||
139
daemons/logd/LogDaemon.cpp
Normal file
139
daemons/logd/LogDaemon.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
//
|
||||
// Log Daemon
|
||||
// Neal Probert
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/timeb.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "UriParse.h"
|
||||
#include "LogDaemon.h"
|
||||
#include "netlib.h"
|
||||
|
||||
//****************************************************************************
|
||||
// defines
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// macros
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// structs & typedefs
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// global constants
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// global variables
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// static constants
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// static variables
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// static functions
|
||||
//***************************************************************************/
|
||||
|
||||
//****************************************************************************
|
||||
// C++ functions
|
||||
//***************************************************************************/
|
||||
|
||||
LogDaemon::LogDaemon()
|
||||
{
|
||||
// logging
|
||||
log_open( LOGD_NAME );
|
||||
}
|
||||
|
||||
LogDaemon::~LogDaemon()
|
||||
{
|
||||
}
|
||||
|
||||
/* UDP ***********************************************************************/
|
||||
|
||||
int LogDaemon::ClientInOut( char *buf, int n, int max )
|
||||
{
|
||||
if ( n > 0 )
|
||||
{
|
||||
Dprintf( "%s\n", buf );
|
||||
Log.LogWrite( buf, n );
|
||||
}
|
||||
|
||||
// no response
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* log ***********************************************************************/
|
||||
|
||||
int LogDaemon::LogOpen( const char *uri )
|
||||
{
|
||||
if ( Log.LogOpen( uri ) )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int LogDaemon::LogPlayback( void )
|
||||
{
|
||||
timeval tv;
|
||||
tv.tv_sec = 0L;
|
||||
tv.tv_usec = 0L;
|
||||
|
||||
// read log input
|
||||
// send message
|
||||
// if ( strlen( logbuf ) )
|
||||
// ClientCast( logbuf, strlen(logbuf) );
|
||||
|
||||
// read next message
|
||||
if ( Log.LogRead( &tv, logbuf, sizeof(logbuf) ) > 0 )
|
||||
{
|
||||
// is this first one?
|
||||
if ( logcount == 0 )
|
||||
{
|
||||
// fudge it so it kicks off 10 seconds later
|
||||
loglast.tv_sec = tv.tv_sec - 10;
|
||||
loglast.tv_usec = tv.tv_usec;
|
||||
}
|
||||
logcount++;
|
||||
|
||||
// diff between this one and last
|
||||
if ( loglast.tv_usec > tv.tv_usec )
|
||||
{
|
||||
// carry
|
||||
tv.tv_sec--;
|
||||
tv.tv_usec += 1000000L;
|
||||
}
|
||||
timeval sv = tv;
|
||||
|
||||
// diff is timeout period
|
||||
tv.tv_sec = tv.tv_sec - loglast.tv_sec;
|
||||
tv.tv_usec = tv.tv_usec - loglast.tv_usec;
|
||||
|
||||
// mark last log record
|
||||
loglast = sv;
|
||||
}
|
||||
else
|
||||
return 1;
|
||||
|
||||
// set next timeout
|
||||
SetTimeout( tv );
|
||||
return 1;
|
||||
}
|
||||
74
daemons/logd/LogDaemon.h
Normal file
74
daemons/logd/LogDaemon.h
Normal file
@ -0,0 +1,74 @@
|
||||
//
|
||||
// Log Daemon
|
||||
// Neal Probert
|
||||
//
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __LogDaemon__
|
||||
#define __LogDaemon__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include "UdpDaemon.h"
|
||||
#include "LogData.h"
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
#define LOGD_NAME "logd"
|
||||
#define LOGD_SERVICE "logd"
|
||||
#ifndef LOGD_PORT
|
||||
#define LOGD_PORT "2940"
|
||||
#endif
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
//
|
||||
// Listens to OBE HeartBeat messages
|
||||
//
|
||||
class LogDaemon : public UdpDaemon {
|
||||
// public data
|
||||
public:
|
||||
|
||||
// private data
|
||||
private:
|
||||
// Logging output
|
||||
LogData Log;
|
||||
|
||||
// log playback
|
||||
char logbuf[512];
|
||||
timeval loglast;
|
||||
unsigned int logcount;
|
||||
|
||||
// private methods
|
||||
|
||||
// static data
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
LogDaemon();
|
||||
|
||||
// destructor
|
||||
virtual ~LogDaemon();
|
||||
|
||||
// virtual functions
|
||||
int ClientInOut( char *buf, int n, int max );
|
||||
|
||||
// public methods
|
||||
int LogOpen( const char *uri );
|
||||
int LogWrite( const char *log ) { return Log.LogWrite(log,strlen(log)); };
|
||||
void SetBinary(void) { Log.SetBinary(); };
|
||||
|
||||
int LogPlayback( void );
|
||||
|
||||
// static methods
|
||||
|
||||
// private methods
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
145
daemons/logd/logd.cpp
Normal file
145
daemons/logd/logd.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
//
|
||||
// Serial Daemon (similar to ser2net)
|
||||
// Neal Probert
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "LogDaemon.h"
|
||||
|
||||
#include "netlib.h"
|
||||
|
||||
extern int optind, opterr, optopt;
|
||||
|
||||
//
|
||||
// any port
|
||||
//
|
||||
|
||||
static const char *helptext =
|
||||
"logd [options]\n"
|
||||
"\t-d debug (foreground with stdio)\n"
|
||||
"\t-h help\n"
|
||||
"\t-i infile input log format:/path/to/file.log\n"
|
||||
"\t-o logfile output log format:/path/to/file.log\n"
|
||||
"\t-S service UDP service\n";
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char c; /* we have character */
|
||||
|
||||
LogDaemon Log;
|
||||
|
||||
/* options */
|
||||
int debug = 0; // run in foreground, leaves stdio open
|
||||
|
||||
/* connections */
|
||||
const char *file = NULL;
|
||||
const char *log = NULL;
|
||||
const char *service = LOGD_PORT;
|
||||
|
||||
// add command line args
|
||||
while ( (c = getopt( argc, argv, "bdhi:o:S:" )) > 0 )
|
||||
{
|
||||
switch ( c )
|
||||
{
|
||||
case 'b':
|
||||
Log.SetBinary();
|
||||
break;
|
||||
case 'd':
|
||||
debug = 1;
|
||||
break;
|
||||
case 'h':
|
||||
puts( helptext );
|
||||
return 0;
|
||||
case 'i':
|
||||
file = optarg;
|
||||
break;
|
||||
case 'o':
|
||||
log = optarg;
|
||||
break;
|
||||
case 'S':
|
||||
service = optarg;
|
||||
break;
|
||||
default:
|
||||
fprintf( stderr, "Unknown option -%c\n", c );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if ( log )
|
||||
{
|
||||
if ( Log.LogOpen( log ) < 0 )
|
||||
{
|
||||
fprintf( stderr, "Unable to open log file: %s!\n", log );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf( stderr, "Must specify a MySQL connection: mysql://user:pass@host/database/table\n" );
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ( file )
|
||||
{
|
||||
char buf[4096];
|
||||
int n = 0;
|
||||
|
||||
if ( strcmp( file, "-") == 0 )
|
||||
{
|
||||
while ( fgets(buf, sizeof(buf), stdin) )
|
||||
{
|
||||
Log.LogWrite( buf );
|
||||
n++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// import
|
||||
FILE *import = fopen( file, "r" );
|
||||
if ( import )
|
||||
{
|
||||
// read into database
|
||||
while ( fgets( buf, sizeof(buf), import ) )
|
||||
{
|
||||
// check for header?
|
||||
|
||||
Log.LogWrite( buf );
|
||||
n++;
|
||||
}
|
||||
printf( "Imported %d lines into log\n", n );
|
||||
log_printf( "Imported %d lines into log\n", n );
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf( stderr, "Unable to import log data: %s\n", file );
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// UDP listener
|
||||
if ( Log.StartService( LOGD_NAME, "0.0.0.0", service, debug ) >= 0 )
|
||||
return Log.RunService();
|
||||
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user