Skip to content

Commit

Permalink
Uptime HTTP server, displays uptime information as response to HTTP r…
Browse files Browse the repository at this point in the history
…equest
  • Loading branch information
René Feiner committed Aug 24, 2015
0 parents commit 1437ec8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BINARY = bin/uptime-httpd
GO = env GOPATH="$(PWD)/vendor:$(PWD)" go

all:
@echo "make release # Build a release version"
@echo "make run # Run a development version"

release: clean
@env GOOS=linux GOARCH=amd64 $(GO) build -o $(BINARY) uptime-httpd.go

run: clean
@$(GO) build -o $(BINARY) uptime-httpd.go
@./$(BINARY)

clean:
rm -fr bin pkg vendor/pkg
65 changes: 65 additions & 0 deletions init.d/uptime-httpd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#! /bin/sh

### BEGIN INIT INFO
# Provides: uptime-httpd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Uptime HTTPd
# Description: Starts the Uptime HTTPd service in the background
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

GOPATH="/path/to/this/project"

UHTTPD="$GOPATH/bin/uptime-httpd"
UHTTPD_NAME="uptime-httpd"
UHTTPD_PID="/var/run/$UHTTPD_NAME.pid"
UHTTPD_LOG_DIR="$/var/log/$UHTTPD_NAME" ; mkdir -p $UHTTPD_LOG_DIR
UHTTPD_LOG_FILE="$UHTTPD_LOG_DIR/$UHTTPD_NAME.log"

SSD_PARAMS="--quiet --oknodo --pidfile $UHTTPD_PID"

set -e
test -x $UHTTPD || exit 0

. /lib/lsb/init-functions

case "$1" in
start)
log_daemon_msg "Starting Uptime HTTPd service" "$UHTTPD_NAME" || true
if start-stop-daemon --start $SSD_PARAMS --make-pidfile --background --no-close --exec $UHTTPD >> $UHTTPD_LOG_FILE 2>&1; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;

stop)
log_daemon_msg "Stopping Uptime HTTPd service" "$UHTTPD_NAME" || true
if start-stop-daemon --stop --retry 10 $SSD_PARAMS; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;

restart)
$0 stop
$0 start
;;

status)
status_of_proc -p $UHTTPD_PID $UHTTPD $UHTTPD_NAME && exit 0 || exit $?
;;

*)
log_action_msg "Usage: /etc/init.d/$UHTTPD_NAME {start|stop|restart|status}" || true
exit 1
esac

exit 0
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# _uptime_ HTTP server

## Features
* Returns _uptime_ of server running the uptime-http-server
* Define listening socket using _-socket_

## Usage
```
./uptime-httpd [-socket=":80"]
```

## Todo (someday)
* Request logging

## Author
* [René Feiner](http://feiner.nu) for internal usage at [MessageBird](https://www.messagebird.com)

## Thanks to
* [Maurice Nonnekes](https://github.com/prep)
28 changes: 28 additions & 0 deletions uptime-httpd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"net/http"
"log"
"flag"
"os/exec"
)

var bindSocket = flag.String("socket", ":80", "Define socket where to listen for the HTTP requests")

func handler(w http.ResponseWriter, r *http.Request) {
out, err := exec.Command("/usr/bin/uptime").Output()
if err != nil {
fmt.Fprintf(w, "Error, unable to retrieve uptime: %s", err)
log.Fatal(err)
}
fmt.Fprintf(w, "%s", out)
}

func main() {
flag.Parse()
fmt.Printf("Starting server at %s\n", *bindSocket)

http.HandleFunc("/", handler)
http.ListenAndServe(*bindSocket, nil)
}

0 comments on commit 1437ec8

Please sign in to comment.