Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c110403a0d | |||
| b913d49317 | |||
| 8b23731451 | |||
| 86723f24ca | |||
| a52fb3c43d | |||
| ef2dd31219 | |||
| 12b4086bde | |||
| 4aa0beb795 | |||
| 84ab627110 | |||
| 45dff6eab9 | |||
| 0394f131be | |||
| 108b04acab | |||
| 27ad91bc71 | |||
| e9327ba859 |
52
__dist/README.txt
Normal file
52
__dist/README.txt
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
A logging service for NMDC hubs.
|
||||||
|
|
||||||
|
It logs public chat messages to a file, categorised by months. Binaries are provided for Windows/Linux amd64/i386.
|
||||||
|
|
||||||
|
Written in golang
|
||||||
|
Tags: nmdc
|
||||||
|
|
||||||
|
=USAGE=
|
||||||
|
|
||||||
|
`$nmdc-log-service -Help
|
||||||
|
|
||||||
|
Usage of nmdc-log-service:
|
||||||
|
-Debug
|
||||||
|
Print additional information on stdout
|
||||||
|
-Dir string
|
||||||
|
Output directory (default ".")
|
||||||
|
-LogConnectionState
|
||||||
|
Include connection state changes in log (default true)
|
||||||
|
-Nick string
|
||||||
|
Nick (default "nmdc-log-service")
|
||||||
|
-PMResponse string
|
||||||
|
Message to respond with on PM (default "This is an automated service. For enquiries, please contact an administrator.")
|
||||||
|
-Password string
|
||||||
|
Registered nick password
|
||||||
|
-Server string
|
||||||
|
Addresses to connect to (comma-separated)
|
||||||
|
-VerifyTLS
|
||||||
|
Verify TLS certificates (default true)
|
||||||
|
`
|
||||||
|
|
||||||
|
=CHANGELOG=
|
||||||
|
|
||||||
|
2016-04-16 1.0.4
|
||||||
|
- Enhancement: Upgrade `libnmdc` from `r5` to `r6`
|
||||||
|
- Include a sample systemd unit script in source archive
|
||||||
|
- Fix an issue showing zero connected hubs in user tag
|
||||||
|
- Fix a cosmetic issue with logging repeated identical connection failure messages
|
||||||
|
|
||||||
|
2016-04-04 1.0.3
|
||||||
|
- Enhancement: Upgrade `libnmdc` from `r4` to `r5`
|
||||||
|
|
||||||
|
2016-04-03 1.0.2
|
||||||
|
- Enhancement: Upgrade `libnmdc` from `r3` to `r4`
|
||||||
|
|
||||||
|
2016-04-03 1.0.1
|
||||||
|
- Enhancement: Add `-VerifyTLS` option
|
||||||
|
- Enhancement: Upgrade `libnmdc` from `r2` to `r3`
|
||||||
|
- Fix an issue writing log files on Linux
|
||||||
|
- Fix a cosmetic issue with error message formatting
|
||||||
|
|
||||||
|
2016-04-02 1.0.0
|
||||||
|
- Initial public release
|
||||||
18
build.sh
18
build.sh
@@ -7,6 +7,12 @@ export GOPATH=$(
|
|||||||
cygpath -w "$(pwd)"
|
cygpath -w "$(pwd)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sanitise() {
|
||||||
|
local tmp=$(mktemp)
|
||||||
|
cat "$1" | perl -pe 's~C:.Users.......Documents.DEV.~C:/xxxxxxxxxxxxxxxxxxxxxxxxx/~g' > "$tmp"
|
||||||
|
mv "$tmp" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
|
||||||
local version=""
|
local version=""
|
||||||
@@ -24,22 +30,26 @@ main() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Building win64..."
|
echo "Building win64..."
|
||||||
GOARCH=amd64 GOOS=windows go build -ldflags -s -o nmdc-log-service.exe
|
GOARCH=amd64 GOOS=windows go build -a -ldflags -s -o nmdc-log-service.exe
|
||||||
|
sanitise nmdc-log-service.exe
|
||||||
7z a -mx9 nmdc-log-service-${version}-win64.7z nmdc-log-service.exe >/dev/null
|
7z a -mx9 nmdc-log-service-${version}-win64.7z nmdc-log-service.exe >/dev/null
|
||||||
rm ./nmdc-log-service.exe
|
rm ./nmdc-log-service.exe
|
||||||
|
|
||||||
echo "Building win32..."
|
echo "Building win32..."
|
||||||
GOARCH=386 GOOS=windows go build -ldflags -s -o nmdc-log-service.exe
|
GOARCH=386 GOOS=windows go build -a -ldflags -s -o nmdc-log-service.exe
|
||||||
|
sanitise nmdc-log-service.exe
|
||||||
7z a -mx9 nmdc-log-service-${version}-win32.7z nmdc-log-service.exe >/dev/null
|
7z a -mx9 nmdc-log-service-${version}-win32.7z nmdc-log-service.exe >/dev/null
|
||||||
rm ./nmdc-log-service.exe
|
rm ./nmdc-log-service.exe
|
||||||
|
|
||||||
echo "Building linux64..."
|
echo "Building linux64..."
|
||||||
GOARCH=amd64 GOOS=linux go build -ldflags -s -o nmdc-log-service
|
GOARCH=amd64 GOOS=linux go build -a -ldflags -s -o nmdc-log-service
|
||||||
|
sanitise nmdc-log-service
|
||||||
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux64.tar.xz nmdc-log-service --owner=0 --group=0
|
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux64.tar.xz nmdc-log-service --owner=0 --group=0
|
||||||
rm ./nmdc-log-service
|
rm ./nmdc-log-service
|
||||||
|
|
||||||
echo "Building linux32..."
|
echo "Building linux32..."
|
||||||
GOARCH=386 GOOS=linux go build -ldflags -s -o nmdc-log-service
|
GOARCH=386 GOOS=linux go build -a -ldflags -s -o nmdc-log-service
|
||||||
|
sanitise nmdc-log-service
|
||||||
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux32.tar.xz nmdc-log-service --owner=0 --group=0
|
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux32.tar.xz nmdc-log-service --owner=0 --group=0
|
||||||
rm ./nmdc-log-service
|
rm ./nmdc-log-service
|
||||||
|
|
||||||
|
|||||||
14
hublog.service
Normal file
14
hublog.service
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=NMDC Log Service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Environment=TZ=Etc/UTC
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/home/hublog/nmdc-log-service -Dir /home/hublog/logs/ -Server ... -Nick ... -Password '...' -VerifyTLS=false
|
||||||
|
Restart=always
|
||||||
|
RestartSec=30s
|
||||||
|
User=hublog
|
||||||
|
Group=nogroup
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
37
main.go
37
main.go
@@ -15,6 +15,7 @@ var BaseDir string = "."
|
|||||||
var PMResponse string = ""
|
var PMResponse string = ""
|
||||||
var LogConnectionState bool
|
var LogConnectionState bool
|
||||||
var DebugMode bool
|
var DebugMode bool
|
||||||
|
var VerifyTLS bool
|
||||||
var CharacterMatcher *regexp.Regexp
|
var CharacterMatcher *regexp.Regexp
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -36,7 +37,7 @@ func LogMessage(hub, message string) {
|
|||||||
// Produce a timestamp
|
// Produce a timestamp
|
||||||
timeStamp := instant.Format("2006-01-02 15:04:05")
|
timeStamp := instant.Format("2006-01-02 15:04:05")
|
||||||
|
|
||||||
fh, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE, 0644)
|
fh, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error())
|
fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error())
|
||||||
return
|
return
|
||||||
@@ -50,12 +51,17 @@ func LogMessage(hub, message string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HubWorker(addr, nick, password string) {
|
type HubWorker struct {
|
||||||
|
LastConnectionStateMessage string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *HubWorker) MainLoop(addr, nick, password string) {
|
||||||
|
|
||||||
opts := libnmdc.HubConnectionOptions{
|
opts := libnmdc.HubConnectionOptions{
|
||||||
Address: libnmdc.HubAddress(addr),
|
Address: libnmdc.HubAddress(addr),
|
||||||
Self: libnmdc.UserInfo{Nick: nick},
|
SkipVerifyTLS: !VerifyTLS,
|
||||||
NickPassword: password,
|
Self: *libnmdc.NewUserInfo(nick),
|
||||||
|
NickPassword: password,
|
||||||
}
|
}
|
||||||
hub := opts.Connect()
|
hub := opts.Connect()
|
||||||
|
|
||||||
@@ -69,11 +75,16 @@ func HubWorker(addr, nick, password string) {
|
|||||||
switch event.EventType {
|
switch event.EventType {
|
||||||
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
|
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
|
||||||
if LogConnectionState {
|
if LogConnectionState {
|
||||||
|
str := "* " + event.StateChange.Format()
|
||||||
if len(event.Message) > 0 {
|
if len(event.Message) > 0 {
|
||||||
LogMessage(addr, "* "+event.StateChange.Format()+" ("+event.Message+")")
|
str += " (" + event.Message + ")"
|
||||||
} else {
|
|
||||||
LogMessage(addr, "* "+event.StateChange.Format())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prevent logging the same message repeatedly
|
||||||
|
if str != this.LastConnectionStateMessage {
|
||||||
|
LogMessage(addr, str)
|
||||||
|
}
|
||||||
|
this.LastConnectionStateMessage = str
|
||||||
}
|
}
|
||||||
|
|
||||||
case libnmdc.EVENT_PUBLIC:
|
case libnmdc.EVENT_PUBLIC:
|
||||||
@@ -104,18 +115,23 @@ func main() {
|
|||||||
flag.BoolVar(&LogConnectionState, "LogConnectionState", true, "Include connection state changes in log")
|
flag.BoolVar(&LogConnectionState, "LogConnectionState", true, "Include connection state changes in log")
|
||||||
flag.StringVar(&PMResponse, "PMResponse", "This is an automated service. For enquiries, please contact an administrator.", "Message to respond with on PM")
|
flag.StringVar(&PMResponse, "PMResponse", "This is an automated service. For enquiries, please contact an administrator.", "Message to respond with on PM")
|
||||||
flag.BoolVar(&DebugMode, "Debug", false, "Print additional information on stdout")
|
flag.BoolVar(&DebugMode, "Debug", false, "Print additional information on stdout")
|
||||||
|
flag.BoolVar(&VerifyTLS, "VerifyTLS", true, "Verify TLS certificates")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Assert dir exists
|
// Assert dir exists
|
||||||
dinfo, err := os.Stat(BaseDir)
|
dinfo, err := os.Stat(BaseDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "FATAL: %s", err.Error())
|
fmt.Fprintf(os.Stderr, "FATAL: %s\n", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if !dinfo.IsDir() {
|
if !dinfo.IsDir() {
|
||||||
fmt.Fprintf(os.Stderr, "FATAL: Path '%s' is not a directory\n", BaseDir)
|
fmt.Fprintf(os.Stderr, "FATAL: Path '%s' is not a directory\n", BaseDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !VerifyTLS {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARNING: TLS certificates will not be verified!\n")
|
||||||
|
}
|
||||||
|
|
||||||
// Launch loggers
|
// Launch loggers
|
||||||
all_hubs := strings.Split(*hubs, ",")
|
all_hubs := strings.Split(*hubs, ",")
|
||||||
launch_ct := 0
|
launch_ct := 0
|
||||||
@@ -128,7 +144,8 @@ func main() {
|
|||||||
os.MkdirAll(GetDirectoryNameForHub(hubaddr), 0755)
|
os.MkdirAll(GetDirectoryNameForHub(hubaddr), 0755)
|
||||||
|
|
||||||
// Launch logger
|
// Launch logger
|
||||||
go HubWorker(hubaddr, *nick, *password)
|
hw := HubWorker{}
|
||||||
|
go hw.MainLoop(hubaddr, *nick, *password)
|
||||||
|
|
||||||
launch_ct++
|
launch_ct++
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user