1
0
httpprocprobed/main.go

129 lines
3.3 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
)
// listWatchedProcesses prints a list of processes being watched.
func listWatchedProcesses(processes *[]string) {
for _, v := range *processes {
fmt.Printf("%s, ", v)
}
fmt.Println()
}
// version prints information about program.
func version() {
fmt.Println("httpprocwatchd ver. 1.3.2")
fmt.Println("Copyright (c) 2021,2022 Alexander \"Arav\" Andreev <me@arav.top>")
fmt.Println("This program is licensed under terms of MIT+NIGGER license a copy")
fmt.Println("of wich is located in /usr/share/licenses/httpprocwatchd/LICENSE.")
}
func main() {
var oConfigPath string
var oShowVersion bool
var oListProcesses bool
var oAddProcess string
var oRemoveProcess string
log.SetFlags(0)
flag.StringVar(&oConfigPath, "config", "config.conf", "path to configuration file")
flag.StringVar(&oConfigPath, "c", "config.conf", "path to configuration file (shorthand)")
flag.BoolVar(&oShowVersion, "version", false, "show version")
flag.BoolVar(&oShowVersion, "v", false, "show version (shorthand)")
flag.BoolVar(&oListProcesses, "list", false, "list watched processes")
flag.BoolVar(&oListProcesses, "l", false, "list watched processes (shorthand)")
flag.StringVar(&oAddProcess, "add", "", "add process to list")
flag.StringVar(&oAddProcess, "a", "", "add process to list (shorthand)")
flag.StringVar(&oRemoveProcess, "remove", "", "remove process from list")
flag.StringVar(&oRemoveProcess, "r", "", "remove process from list (shorthand)")
flag.Parse()
if oShowVersion {
version()
return
}
conf, err := LoadConfiguration(oConfigPath)
if err != nil {
log.Fatalf("Cannot load configuration file: %s\n", err)
}
if oListProcesses {
listWatchedProcesses(&conf.Processes)
return
}
if oAddProcess != "" {
err := conf.AddProcessToList(oAddProcess, oConfigPath)
if err != nil {
log.Fatalf("Cannot add process: %s\n", err)
}
}
if oRemoveProcess != "" {
err := conf.RemoveProcessFromList(oRemoveProcess, oConfigPath)
if err != nil {
log.Fatalf("Cannot remove process: %s\n", err)
}
}
// If we modified a list then let's look for a running program and
// send SIGHUP for it to reload a list.
if oAddProcess != "" || oRemoveProcess != "" {
pids, _ := GetProcessPIDs("httpprocwatchd")
if pids != nil && len(pids) > 1 {
var trgt_pid int
if pids[0] == os.Getpid() {
trgt_pid = pids[1]
} else {
trgt_pid = pids[0]
}
if proc, err := os.FindProcess(trgt_pid); err == nil {
proc.Signal(syscall.SIGHUP)
}
}
return
}
srv := CreateAndStartHTTPServer(conf)
syssignal := make(chan os.Signal, 1)
signal.Notify(syssignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
log.Printf("httpprocwatchd is running on \"%s\".", conf.ListenAddress)
for {
switch <-syssignal {
case os.Interrupt:
fallthrough
case syscall.SIGINT | syscall.SIGTERM:
log.Println("Shutting down... ")
ShutdownHTTPServer(srv)
log.Println("Server shutted down.")
os.Exit(0)
case syscall.SIGHUP:
newconf, err := LoadConfiguration(oConfigPath)
if err != nil {
log.Fatalf("Failed to reload a list of processes from configuration: %s\n", err)
}
conf.Processes = newconf.Processes
log.Println("Successfully reloaded a list of watched processes.")
}
}
}