1
0
Fork 0

Compare commits

...

7 Commits

8 changed files with 78 additions and 74 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2021,2022 Alexander "Arav" Andreev <me@arav.top> Copyright (c) 2021-2023 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

@ -2,7 +2,9 @@ TARGET=httpprocprobed
SYSDDIR_=${shell pkg-config systemd --variable=systemdsystemunitdir} SYSDDIR_=${shell pkg-config systemd --variable=systemdsystemunitdir}
SYSDDIR=${SYSDDIR_:/%=%} SYSDDIR=${SYSDDIR_:/%=%}
DESTDIR:= DESTDIR:=
PREFIX:=/usr/local
VERSION:=3.0.0 VERSION:=3.0.0
@ -17,15 +19,15 @@ ${TARGET}: ${SOURCES}
go build ${LDFLAGS} ${FLAGS} go build ${LDFLAGS} ${FLAGS}
install: install:
install -Dm 0755 ${TARGET} ${DESTDIR}usr/bin/${TARGET} install -Dm 0755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
install -Dm 0644 configs/config.example.json ${DESTDIR}etc/${TARGET}.json install -Dm 0644 configs/config.example.json ${DESTDIR}${PREFIX}etc/${TARGET}.json
install -Dm 0644 LICENSE ${DESTDIR}usr/share/licenses/${TARGET}/LICENSE install -Dm 0644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
install -Dm 0644 init/systemd.service ${DESTDIR}${SYSDDIR}/${TARGET}.service install -Dm 0644 init/systemd.service ${DESTDIR}${PREFIX}${SYSDDIR}/${TARGET}.service
uninstall: uninstall:
rm ${DESTDIR}usr/bin/${TARGET} rm ${DESTDIR}${PREFIX}/bin/${TARGET}
rm ${DESTDIR}usr/share/licenses/${TARGET} rm ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
rm ${DESTDIR}${SYSDDIR}/${TARGET}.service rm ${DESTDIR}${PREFIX}${SYSDDIR}/${TARGET}.service
clean: clean:
go clean rm httpprocprobed

View File

@ -9,15 +9,19 @@ license=('MIT')
makedepends=('go>=1.17') makedepends=('go>=1.17')
backup=('etc/httpprocprobed.json') backup=('etc/httpprocprobed.json')
source=("https://git.arav.su/Arav/httpprocprobed/archive/$pkgver.tar.gz") source=("https://git.arav.su/Arav/httpprocprobed/archive/$pkgver.tar.gz")
noextract=()
md5sums=('SKIP') md5sums=('SKIP')
build() { build() {
cd "$srcdir/$pkgname" cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/" export GOPATH="$srcdir"/gopath
export CGO_CPPFLAGS="${CPPFLAGS}"
export CGO_CFLAGS="${CFLAGS}"
export CGO_CXXFLAGS="${CXXFLAGS}"
export CGO_LDFLAGS="${LDFLAGS}"
make VERSION=$pkgver DESTDIR="$pkgdir/" PREFIX="/usr"
} }
package() { package() {
cd "$srcdir/$pkgname" cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/" install make DESTDIR="$pkgdir/" PREFIX="/usr" install
} }

24
http.go
View File

@ -1,24 +0,0 @@
package main
import (
"encoding/json"
"log"
"net/http"
)
// AreProcessesUp sends back status of watched processes.
func AreProcessesUp(processes *[]Process) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Add("Allow", "GET")
return
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(GetProcessesState(processes)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to encode a process list: %s\n", err)
}
}
}

View File

@ -1,12 +1,12 @@
[Unit] [Unit]
Description=HTTPProcProbeD Description=HTTP Process Prober Daemon
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
DynamicUser=yes DynamicUser=yes
Restart=on-failure Restart=on-failure
ExecStart=/usr/bin/httpprocprobed -c /etc/httpprocprobed.conf ExecStart=/usr/bin/httpprocprobed -c /etc/httpprocprobed.json
ExecReload=kill -HUP $MAINPID ExecReload=kill -HUP $MAINPID
ReadOnlyPaths=/ ReadOnlyPaths=/

18
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -74,3 +75,20 @@ func main() {
} }
} }
} }
// AreProcessesUp sends back status of watched processes.
func AreProcessesUp(processes *[]Process) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Add("Allow", "GET")
return
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(GetProcessesState(processes)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to encode a process list: %s\n", err)
}
}
}

View File

@ -1,11 +1,5 @@
package main package main
import (
"os"
"strconv"
"strings"
)
// Process contains an alias that will be returned when queries, and a process // Process contains an alias that will be returned when queries, and a process
// name to look for. // name to look for.
type Process struct { type Process struct {
@ -26,33 +20,3 @@ func GetProcessesState(procs *[]Process) (ps ProcessesState) {
return return
} }
// GetProcessPIDs returns a list of PIDs found for a process.
func GetProcessPIDs(name string) (pids []int, err error) {
dir, err := os.ReadDir("/proc/")
if err != nil {
return nil, err
}
for _, entry := range dir {
// This line demands a little clarification. Here we are making sure
// that the first character of a directory name is numeric. In ASCII
// all numerals are laying within 0x30-0x39 range.
if entry.IsDir() && entry.Name()[0]>>0x4&0xf == 0x3 {
cmdline, err := os.ReadFile("/proc/" + entry.Name() + "/cmdline")
if err != nil {
return nil, err
}
if strings.Contains(string(cmdline), name) {
pid, err := strconv.Atoi(entry.Name())
if err != nil {
continue
}
pids = append(pids, pid)
}
}
}
return pids, nil
}

40
process_unix.go Normal file
View File

@ -0,0 +1,40 @@
//go:build unix
// +build unix
package main
import (
"os"
"strconv"
"strings"
)
// GetProcessPIDs returns a list of PIDs found for a process.
func GetProcessPIDs(name string) (pids []int, err error) {
dir, err := os.ReadDir("/proc/")
if err != nil {
return nil, err
}
for _, entry := range dir {
// This line demands a little clarification. Here we are making sure
// that the first character of a directory name is numeric. In ASCII
// all numerals are laying within 0x30-0x39 range.
if entry.IsDir() && entry.Name()[0]>>0x4&0xf == 0x3 {
cmdline, err := os.ReadFile("/proc/" + entry.Name() + "/cmdline")
if err != nil {
return nil, err
}
if strings.Contains(string(cmdline), name) {
pid, err := strconv.Atoi(entry.Name())
if err != nil {
continue
}
pids = append(pids, pid)
}
}
}
return pids, nil
}