1
0
Fork 0

Compare commits

...

8 Commits

6 changed files with 65 additions and 73 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2021-2023 Alexander "Arav" Andreev <me@arav.su> Copyright (c) 2021-2024 Alexander "Arav" Andreev <me@arav.su>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -6,7 +6,7 @@ SYSDDIR=${SYSDDIR_:/%=%}
DESTDIR:= DESTDIR:=
PREFIX:=/usr/local PREFIX:=/usr/local
VERSION:=3.1.0 VERSION:=3.1.1
FLAGS:=-buildmode=pie -modcacherw -mod=readonly -trimpath FLAGS:=-buildmode=pie -modcacherw -mod=readonly -trimpath
LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}" -tags netgo LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}" -tags netgo

View File

@ -1,6 +1,6 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.su> # Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=httpprocprobed pkgname=httpprocprobed
pkgver=3.1.0 pkgver=3.1.1
pkgrel=1 pkgrel=1
pkgdesc="HTTPProcProbeD hands out an HTTP endpoint to get if processes are running." pkgdesc="HTTPProcProbeD hands out an HTTP endpoint to get if processes are running."
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64') arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')

View File

@ -1,38 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
type Configuration struct {
ListenAddress string `json:"listen-address"`
Processes []Process `json:"processes"`
}
func LoadConfiguration(path string) (conf *Configuration, err error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
conf = &Configuration{}
if err := json.NewDecoder(f).Decode(conf); err != nil {
return nil, err
}
for i := 0; i < len(conf.Processes); i++ {
if conf.Processes[i].Process == "" {
return nil, fmt.Errorf("an empty process field found")
}
if conf.Processes[i].Alias == "" {
conf.Processes[i].Alias = conf.Processes[i].Process
}
}
return conf, nil
}

72
main.go
View File

@ -18,12 +18,66 @@ var showVersion *bool = flag.Bool("v", false, "show version")
var version string var version string
// Process contains an alias that will be returned when queried, and a process
// name to look for.
// A process is effectively a substring that is being looked in a cmdline file
// in /proc/ dir on unix-like systems.
type Process struct {
Alias string `json:"alias"`
Process string `json:"process"`
}
// ProcessesState is a map of processes' aliases and its statuses.
type ProcessesState map[string]bool
func GetProcessesState(procs *[]Process) (ps ProcessesState) {
ps = make(ProcessesState)
for _, proc := range *procs {
pids, err := GetProcessPIDs(proc.Process)
ps[proc.Alias] = err == nil && len(pids) > 0
}
return
}
type Configuration struct {
ListenAddress string `json:"listen-address"`
Processes []Process `json:"processes"`
}
func LoadConfiguration(path string) (conf *Configuration, err error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
conf = &Configuration{}
if err := json.NewDecoder(f).Decode(conf); err != nil {
return nil, err
}
for i := 0; i < len(conf.Processes); i++ {
if conf.Processes[i].Process == "" {
return nil, fmt.Errorf("an empty process field found")
}
if conf.Processes[i].Alias == "" {
conf.Processes[i].Alias = conf.Processes[i].Process
}
}
return conf, nil
}
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
flag.Parse() flag.Parse()
if *showVersion { if *showVersion {
fmt.Println("httpprocprobed ver.", version, "Copyright (c) 2021-2023 Alexander \"Arav\" Andreev <me@arav.su>") fmt.Fprintln(os.Stderr, "httpprocprobed ver.", version, "\nCopyright (c) 2021-2024 Alexander \"Arav\" Andreev <me@arav.su>")
os.Exit(0) os.Exit(0)
} }
@ -48,30 +102,28 @@ func main() {
log.Fatalf("ListenAndServe: %s\n", err) log.Fatalf("ListenAndServe: %s\n", err)
} }
}() }()
defer func() {
srv.SetKeepAlivesEnabled(false)
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatalf("%s\n", err)
}
}()
syssignal := make(chan os.Signal, 1) syssignal := make(chan os.Signal, 1)
signal.Notify(syssignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) signal.Notify(syssignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
log.Printf("httpprocprobed is running on \"%s\".", conf.ListenAddress)
for { for {
switch <-syssignal { switch <-syssignal {
case os.Interrupt: case os.Interrupt:
fallthrough fallthrough
case syscall.SIGINT | syscall.SIGTERM: case syscall.SIGINT | syscall.SIGTERM:
srv.SetKeepAlivesEnabled(false) return
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatalf("%s\n", err)
}
log.Println("Server shutted down.")
os.Exit(0)
case syscall.SIGHUP: case syscall.SIGHUP:
newconf, err := LoadConfiguration(*configPath) newconf, err := LoadConfiguration(*configPath)
if err != nil { if err != nil {
log.Fatalf("Failed to reload a list of processes from configuration: %s\n", err) log.Fatalf("Failed to reload a list of processes from configuration: %s\n", err)
} }
conf.Processes = newconf.Processes conf.Processes = newconf.Processes
log.Println("Successfully reloaded a list of watched processes.")
} }
} }
} }

View File

@ -1,22 +0,0 @@
package main
// Process contains an alias that will be returned when queries, and a process
// name to look for.
type Process struct {
Alias string `json:"alias"`
Process string `json:"process"`
}
// ProcessesState is a map of processes' aliases and its statuses.
type ProcessesState map[string]bool
func GetProcessesState(procs *[]Process) (ps ProcessesState) {
ps = make(ProcessesState)
for _, proc := range *procs {
pids, err := GetProcessPIDs(proc.Process)
ps[proc.Alias] = err == nil && len(pids) > 0
}
return
}