A small procedure to send syslog messages (with program)
Posted: Sat Feb 06, 2010 1:27 pm
Hi,
since I want to generate syslog messages, I wrote a small procedure for it.
It is not 100% RFC conform, since at least the version ("1 ") should be inside,
but it looks not nice in linux /var/log/messages, and so I commented it out.
It works in Windo.. and in Linux. And it should work on Mac.
Have fun.
Bernd
P.S.: to receive syslog messages in windo.. I use the great tftpd32
since I want to generate syslog messages, I wrote a small procedure for it.
It is not 100% RFC conform, since at least the version ("1 ") should be inside,
but it looks not nice in linux /var/log/messages, and so I commented it out.
It works in Windo.. and in Linux. And it should work on Mac.
Code: Select all
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
#Version = "1.10"
Enumeration Severity
#Emergency ; system is unusable
#Alert ; action must be taken immediately
#Critical ; critical conditions
#Error ; error conditions
#Warning ; warning conditions
#Notice ; normal but significant condition
#Informational ; informational messages
#Debug ; Debug-level messages
EndEnumeration
Enumeration Facility
#kernel_messages
#user_level_messages
#mail_system
#System_daemons
#security_authorization_messages
#messages_generated_internally_by_syslogd
#line_printer_subsystem
#network_news_subsystem
#UUCP_subsystem
#clock_daemon
#security_authorization_messages_2
#FTP_daemon
#NTP_subsystem
#log_audit
#log_alert
#clock_daemon_2
#local0
#local1
#local2
#local3
#local4
#local5
#local6
#local7
EndEnumeration
Procedure Syslog(Server$, Text$, Severity.a, Facility.a=#local0, Port.i=514)
Protected Result.i, Con.i, Msg$
If Facility < 24 And Severity < 8
Con = OpenNetworkConnection(Server$, Port, #PB_Network_UDP)
If Con
Msg$ = "<" + StrU(Facility << 3 | Severity, #PB_Byte) + ">"
;Msg$ + "1 "
;Msg$ + FormatDate("%yyyy-%mm-%ddT%hh:%ii:%ss.000Z ", Date())
Msg$ + GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + ": "
Msg$ + Text$
If SendNetworkString(Con, Msg$) = StringByteLength(Msg$, #PB_UTF8)
Result = #True
EndIf
CloseNetworkConnection(Con)
EndIf
EndIf
ProcedureReturn Result
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Procedure Usage()
PrintN("")
PrintN(" Syslog V" + #VERSION)
PrintN("")
PrintN(" usage: syslog server message [severity [facility]] [-v]")
PrintN("")
PrintN(" server : name or IP address of the syslog server")
PrintN(" message : the message to send")
PrintN(" severity: level of the message (0..7) default is 6 (informational)")
PrintN(" facility: facility of the sender (0..24) default is 16 (local0)")
PrintN(" -v : increase verbosity")
PrintN("")
PrintN(" example: syslog 192.168.0.1 " + #DQUOTE$ + "This is a test" + #DQUOTE$)
PrintN("")
End 1
EndProcedure
Define Severity.i, Facility.i, Verbose.i, Server$, Msg$, Index.i, i.i, Result.i
If OpenConsole()
If CountProgramParameters() < 2 : Usage() : EndIf
Severity = #Informational
Facility = #local0
Server$ = ProgramParameter(0)
Msg$ = ProgramParameter(1)
For i = 2 To CountProgramParameters() - 1
If ProgramParameter(i) = "-v"
Verbose + 1
Else
Select Index
Case 0
Severity = Val(ProgramParameter(i))
If Severity < #Emergency Or Severity > #Debug
Severity = #Informational
EndIf
Index + 1
Case 1
Facility = Val(ProgramParameter(i))
If Facility < #kernel_messages Or Facility > #local7
Facility = #local0
EndIf
Index + 1
EndSelect
EndIf
Next i
CompilerIf #PB_Compiler_Version < 610
If InitNetwork()
CompilerEndIf
If Verbose
PrintN("")
PrintN(" Server : " + Server$)
PrintN(" Severity: " + Str(Severity))
PrintN(" Facility: " + Str(Facility))
PrintN(" Message : " + Msg$)
PrintN("")
EndIf
Result = Syslog(Server$, Msg$, Severity, Facility)
If Verbose
If Result
PrintN(" message transmitted")
Else
PrintN(" an error occured")
EndIf
PrintN("")
EndIf
CompilerIf #PB_Compiler_Version < 610
Else
PrintN("")
PrintN("was not able to initialize the network")
EndIf
CompilerEndIf
EndIf
End Result
CompilerEndIf
Bernd
P.S.: to receive syslog messages in windo.. I use the great tftpd32