dot

packages and services management
Log | Files | Refs | README

commit 33685adbe2f92ada1910cc1c4d4e4c9e034871e8
parent 8558343a61ea8e2bce5a2e3da71d827e898116e1
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Sun, 23 Jul 2017 12:35:52 +0200

first version of a monitor script

Hopefully compatible cross Linux distros, with {Free,Open}BSD and
maybe even with solaris a bit.

Diffstat:
Abin/monitor | 48++++++++++++++++++++++++++++++++++++++++++++++++
Mbin/status | 133++++++++++++++++++++++---------------------------------------------------------
Mbin/www-git | 42++++++++++++++----------------------------
Mbin/www-git.1 | 34++++++++++++++++++++--------------
Mdarkhttpd/start | 2+-
5 files changed, 120 insertions(+), 139 deletions(-)

diff --git a/bin/monitor b/bin/monitor @@ -0,0 +1,48 @@ +# ressources monitoring script + +# It will produce one line with the following values, separated with a space: +# +# 1. hostname 5. load average (1 minute) +# 2. date-time 6. disk I/O +# 3. free memory 7. total free storage +# 4. cpu usage + +case "$(uname)" in +(Linux ) mem=free cpu=id bi=bi bo=bo b='' ;; +(*BSD ) mem=fre cpu=id bi='' bo='' b="$disks" ;; +(Solaris) mem=free cpu=id bi='' bo='' b="$disks" ;; +esac + +vmstat 2 2 | +awk -v mem="$mem" -v cpu="$cpu" -v bi="$bi" -v bo="$bo" -v disks="$disks" ' + +BEGIN { + "date +%Y-%m-%d-%H-%M" | getline date + "hostname" | getline host + "uptime" | getline load + + sub(/.*[Ll]oad[^0-9]*/, "", load) + sub(/[^0-9.].*/, "", load) + + split(disks, diskv, " ") + + while ("df" | getline) + if ($1 ~ "^/dev/") + stor += $4 +} + +NR == 2 { split($0, keys, " ") } + +NR >= 4 { + for (i = split($0, values, " "); i > 0; i--) + table[keys[i]] = values[i] + + diskio = table[bi] + table[bo] + mem = table[mem] + cpu = 100 - table[cpu] + + for (disk in diskv) + diskio += table[disk] + + print host " " date " " mem " " cpu " " load " " diskio " " stor +}' diff --git a/bin/status b/bin/status @@ -1,109 +1,50 @@ -# plain text status line for wherever useful +# ressources monitoring script +# It will produce one line with the following values, separated with a space: +# +# 1. hostname 5. load average (1 minute) +# 2. date-time 6. disk I/O +# 3. free memory 7. total free storage +# 4. cpu usage -SEPARATOR=' | ' -START=' ' -STOP=' ' +case "$(uname)" in +(Linux ) mem=free cpu=id bi=bi bo=bo b='' ;; +(*BSD ) mem=fre cpu=id bi='' bo='' b="$disks" ;; +(Solaris) mem=free cpu=id bi='' bo='' b="$disks" ;; +esac +disks=$(df | sed -r '1d; s|^/dev/([^ ]*)[^ ] .*|\1|' | sort -u | tr '\n' ' ') -# Maildir format, just file count in "$MAIL/new" +vmstat 2 2 | +awk -v mem="$mem" -v cpu="$cpu" -v bi="$bi" -v bo="$bo" -v disks="$disks" ' -mail=$( - if [ -d "$MAIL/new" ] - then - mail="$(find "$MAIL/new" -type f | wc -l | tr -d '\t ')" - mail="${mail#0}" - mail="${mail:+$mail MAIL}" +BEGIN { + "date +%Y-%m-%d-%H-%M" | getline date + "hostname" | getline host + "uptime" | getline load - printf %s "$mail" - fi -) + sub(/.*[Ll]oad[^0-9]*/, "", load) + sub(/[^0-9.].*/, "", load) + split(disks, diskv, " ") -date=$( - date +'%Z %m/%d %H:%M' -) - - -# from neofetch(1) - -memory=$( - case "$(uname -s)" in - (Linux) - while IFS=': ' read -r key value unit - do - case "$key" in - (MemTotal) total=$((value / 1024)) ;; - (MemFree ) used=$(((total - value / 1024))) ;; - esac - done < /proc/meminfo - ;; - - (FreeBSD) - pgsize=$(sysctl -n hw.pagesize) - inactive=$(($(sysctl -n vm.stats.vm.v_inactive_count) * pgsize)) - unused=$(( $(sysctl -n vm.stats.vm.v_free_count) * pgsize)) - cache=$(( $(sysctl -n vm.stats.vm.v_cache_count) * pgsize)) - total=$(( $(sysctl -n hw.physmem) / 1024 / 1024)) - free=$(((inactive + unused + cache) / 1024 / 1024)) - used=$((total - free)) - ;; - - (OpenBSD) - total=$(($(sysctl -n hw.physmem) / 1024 / 1024)) - used=$(($(vmstat | awk 'END{printf $4}') / 1024)) - ;; - esac - - printf 'MEM %s/%sM' "$((used))" "$((total))" -) - - -cpu=$( - printf 'CPU ' - - vmstat 1 2 | awk ' - NR == 2 { - split($0, keys, " ") - } - - NR == 4 { - i = split($0, values, " ") - - for (; i > 0; i--) - table[keys[i]] = values[i] - - printf "%02s%%", 100 - table["id"] - } - ' - - # load average - uptime | sed -r 's/.*[Ll]oad[^0-9]*([0-9.]*).*/ \1/' | tr -d '\n' -) - - -battery=$( - [ -f /sys/class/power_supply/*/capacity ] && - printf 'BAT %d%%' "`cat /sys/class/power_supply/*/capacity`" -) - + while ("df" | getline) + if ($1 ~ "^/dev/") + stor += $4 +} -disk=$( - : -) +NR == 2 { split($0, keys, " ") } +NR >= 4 { + for (i = split($0, values, " "); i > 0; i--) + table[keys[i]] = values[i] -status() -{ - printf %s "$START" - while [ "$#" -gt 0 ] - do - printf %s "$1" - [ "$1" ] && [ $# -gt 1 ] && printf %s "$SEPARATOR" - shift - done - printf %s "$STOP" -} + diskio = table[bi] + table[bo] + mem = table[mem] + cpu = 100 - table[cpu] + for (disk in diskv) + diskio += table[disk] -status "$mail" "$memory" "$cpu" "$battery" "$disk" "$date" + print host " " date " " mem " " cpu " " load " " diskio " " stor +}' diff --git a/bin/www-git b/bin/www-git @@ -41,40 +41,26 @@ commits() files() { - git -C "$1" ls-files | while IFS='' read -r file + git -C "$1" ls-files | while IFS='' read -r path do - path="${file#$1/}" - [ "$path.html" -nt "$1/$file" ] && continue || echo "file $file" - mkdir -p "$(dirname "$path")" - - echo "$HEADER" > "$path.html" - if [ "$(tr -cd '\0' < "$1/$file" | wc -c)" = 0 ] - then sed -r 's|&|\&amp;|g; s|<|\&lt;|g' "$1/$file" - else echo 'binary file' - fi >> "$path.html" - echo "$FOOTER" >> "$path.html" + [ "$path" -nt "$1/$file" ] && git show "master:$path" > "$path" done } -find -L "$1" -type d -name .git | while IFS='' read -r dir -do - repo="${dir%/.git}" - name="${repo##*/}" - - mkdir -p "$name/commit" "$name/file" +name="${1##*/}" - ( - cd "$name" || exit +mkdir -p "git/$name/commit" "git/$name/file" +( + cd "git/$name" || exit - echo "$HEADER <h1><a href="..">&lt;</a> $name</h1>" - tree "$repo" - echo '</pre><hr/><pre>' - log "$repo" - echo "$FOOTER" - ) > "$name/index.html" + printf '%s <h1><a href="..">&lt;</a> %s</h1>' "$HEADER" "$name" + tree "$repo" + printf '<hr/>\n' + log "$repo" + printf '%s\n' "$FOOTER" +) > "$name/index.html" - (cd "$name/file" && files "$repo") - (cd "$name/commit" && commits "$repo") -done +(cd "$name/file" && files "$repo") +(cd "$name/commit" && commits "$repo") diff --git a/bin/www-git.1 b/bin/www-git.1 @@ -21,12 +21,18 @@ The .Nm script creates a static website presenting the files and commits of a git -repository in a subdirectory. +repository. The build is incremential so that it can be triggered at every +commit without large overhead. . .Pp . -The first argument is treated as a path to a repository, and the -second as its text description. +It only requires +.Xr 1 git +command to produce the output, and the various. +. +.Pp +. +The first argument is treated as a path to a repository. . . .Sh FILES @@ -43,10 +49,13 @@ element. An .Pa index.html file will be created in the repository directory, containing a list of -the files, the README file if any, and the commits. +the files and the commits. . -.It Pa ./repository/file/* Ns , Pa ./repository/commit/* -One html page per file and per commit will be created in these subdirectories. +.It Pa ./repository/commit/4279dbc3735e1e38bf33120c9234640d99fee650.html +One html page per commit is created. +. +.It Pa ./repository/file/path/to/the/file +The file content of the . .El . @@ -55,16 +64,13 @@ One html page per file and per commit will be created in these subdirectories. . A script generating an index of multiple repositories might look like this: .Bd -literal -offset indent -mkdir git -cd git - -printf '<!doctype html>\\n<html>\\n<body>\\n' > ./index.html +printf '<!doctype html>\\n<html>\\n<body>\\n<ul>\\n' > ./index.html -printf '<li><a href="">%s</a></li>\n' ../../repo1 repo1 >> index.html -www-git ../../repo1 'Some repository' +printf '<li><a href="">%s</a></li>\n' git/repo1 repo1 >> index.html +www-git /path/to/repo1 -printf '<li><a href="">%s</a></li>\n' ../../repo2 repo2 >> index.html -www-git ../../repo2 'Another one' +printf '<li><a href="">%s</a></li>\n' git/repo2 repo2 >> index.html +www-git /path/to/repo2 printf '</ul>\\n</body>\\n</html>\\n' >> index.html .Ed diff --git a/darkhttpd/start b/darkhttpd/start @@ -1 +1 @@ -darkhttpd $VAR/www & PID=$! +darkhttpd --default-mimetype=text/plain $VAR/www & PID=$!