dot

packages and services management
Log | Files | Refs | README

commit 34e3d888e3e424f1651268dc52a69e9e52e73578
parent 3b6f2c762042dd329563358db0ad4845fc29862e
Author: Josuah Demangeon <mail@josuah.net>
Date:   Thu, 23 Nov 2017 10:32:06 +0100

freshen-up some scripts

Diffstat:
Abin/calendar | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dbin/calendar-import | 43-------------------------------------------
Dbin/calendar-show | 47-----------------------------------------------
Abin/monit | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dbin/monitor | 53-----------------------------------------------------
Dbin/monitor-plot | 47-----------------------------------------------
Rbin/doc-rfc -> bin/rfc | 0
Mbin/www | 12+++---------
8 files changed, 203 insertions(+), 199 deletions(-)

diff --git a/bin/calendar b/bin/calendar @@ -0,0 +1,97 @@ +#!/bin/sh +# awk functions to deal with calendar + +awk_convert=' +BEGIN { + FS = ":"; +} + +{ + gsub("\015", ""); + gsub("\t", " "); +} + +/BEGIN:VEVENT/,/END:VEVENT/ { + if (substr($0, 1, 1) != " ") { + type = $1; + vevent[type] = substr($0, \ + index($0, ":") + 1, length($0) - length($1)); + } else { + vevent[type] = vevent[type] substr($0, 2, length($0) - 1); + } +} + +/END:VEVENT/ { + gsub(/\\n/, " ", vevent["DESCRIPTION"]); + gsub(/\\/, "", vevent["DESCRIPTION"]); + gsub(/\\/, "", vevent["LOCATION"]); + gsub(/\\/, "", vevent["SUMMARY"]); + + printf("%04d/%02d/%02d %02d:%02d %s %s %s\n", + substr(vevent["DTSTART"], 1, 4), + substr(vevent["DTSTART"], 5, 2), + substr(vevent["DTSTART"], 7, 2), + substr(vevent["DTSTART"], 10, 2), + substr(vevent["DTSTART"], 12, 2), + vevent["SUMMARY"], + vevent["LOCATION"], + vevent["DESCRIPTION"]); +}' + +awk_date=' +function bissextile(yrs) +{ + return yrs % 4 == 0 && (yrs % 100 != 0 || yrs % 400 == 0); +} + +function days_per_month(mth, yrs) +{ + if (mth == 2) + return 28 + bissextile(yrs); + else if (mth < 7) + return 30 + mth % 2; + else + return 30 + mth % 2; +} + +function to_sec(yrs, mth, day, hrs, min, sec) +{ + for (mth--; mth > 0; mth--) + day += days_per_month(mth, yrs); + for (yrs--; yrs >= 1970; yrs--) + day += 365 + bissextile(yrs); + return sec + 60 * (min + 60 * (hrs + (24 * day))); +} + +function to_date(fmt, sec) +{ + b = bissextile(1970); + for (yrs = 1970; sec >= 3600 * 24 * (365 + b); yrs++) { + b = bissextile(yrs); + sec -= 3600 * 24 * (365 + bissextile(yrs)); + } + d = days_per_month(1); + for (mth = 1; sec >= 3600 * 24 * d; mth++) { + d = days_per_month(mth); + sec -= 3600 * 24 * d; + } + for (day = 1; sec >= 3600 * 24; day++) + sec -= 3600 * 24; + for (hrs = 0; sec >= 3600; hrs++) + sec -= 3600; + for (min = 0; sec >= 60; min++) + sec -= 60; + return sprintf(fmt, yrs, mth, day, hrs, min, sec); +} + +function rfc3339(str) { + yrs = substr(str, 1, 4); + mth = substr(str, 6, 2); + day = substr(str, 9, 2); + hrs = substr(str, 12, 2); + min = substr(str, 15, 2); + sec = substr(str, 18, 2); + sub("^[^ ]+\\.[0-9]+", "", str); + off = substr(str, 1, 1) == "Z" ? 0 : substr(str, 1, 3); + return to_sec(yrs, mth, day, hrs, min, sec) - off * 3600; +}' diff --git a/bin/calendar-import b/bin/calendar-import @@ -1,43 +0,0 @@ -#!/bin/sh -# store an ical one line per event - -exec awk ' - -BEGIN { - FS = ":"; -} - - -{ - gsub("\015", ""); - gsub("\t", " "); -} - - -/BEGIN:VEVENT/,/END:VEVENT/ { - if (substr($0, 1, 1) != " ") { - type = $1; - vevent[type] = substr($0, \ - index($0, ":") + 1, length($0) - length($1)); - } else { - vevent[type] = vevent[type] substr($0, 2, length($0) - 1); - } -} - - -/END:VEVENT/ { - gsub(/\\n/, " ", vevent["DESCRIPTION"]); - gsub(/\\/, "", vevent["DESCRIPTION"]); - gsub(/\\/, "", vevent["LOCATION"]); - gsub(/\\/, "", vevent["SUMMARY"]); - - printf("%04d/%02d/%02d %02d:%02d %s %s %s\n", - substr(vevent["DTSTART"], 1, 4), - substr(vevent["DTSTART"], 5, 2), - substr(vevent["DTSTART"], 7, 2), - substr(vevent["DTSTART"], 10, 2), - substr(vevent["DTSTART"], 12, 2), - vevent["SUMMARY"], - vevent["LOCATION"], - vevent["DESCRIPTION"]); -}' "$@" | sort -u diff --git a/bin/calendar-show b/bin/calendar-show @@ -1,47 +0,0 @@ -#!/bin/sh - -awk ' -function bissextile(yrs) -{ - return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0); -} - -function days_per_month(mth, yrs) -{ - if (mth == 2) - return 28 + bissextile(yrs); - else if (mth < 7) - return 30 + mth % 2; - else - return 30 + mth % 2; -} - -function to_seconds(yrs, mth, day, hrs, min, sec) -{ - for (mth--; mth > 0; mth--) - day += days_per_month(mth, yrs); - for (yrs--; yrs >= 1970; yrs--) - day += 365 + bissextile(yrs); - return sec + 60 * (min + 60 * (hrs + (24 * (day - 1)))); -} - -function to_date(fmt, sec) -{ - b = bissextile(1970); - for (yrs = 1970; sec >= 3600 * 24 * (365 + b); yrs++) { - b = bissextile(yrs); - sec -= 3600 * 24 * (365 + bissextile(yrs)); - } - d = days_per_month(1); - for (mth = 1; sec >= 3600 * 24 * d; mth++) { - d = days_per_month(mth); - sec -= 3600 * 24 * d; - } - for (day = 1; sec >= 3600 * 24; day++) - sec -= 3600 * 24; - for (hrs = 0; sec >= 3600; hrs++) - sec -= 3600; - for (min = 0; sec >= 60; min++) - sec -= 60; - return sprintf(fmt, yrs, mth, day, hrs, min, sec); -}' diff --git a/bin/monit b/bin/monit @@ -0,0 +1,103 @@ +#!/bin/sh +# ressources monitoring script for plotting with plot(1) + +# It will produce one line with the following values, separated with a space: +# +# 1. hostname 5. load average (1 minute) +# 2. unix time 6. disk I/O +# 3. free memory 7. total free storage +# 4. cpu usage + +DATA=$HOME/var/monit + +awk_record='# -v mem -v bi -v bo +BEGIN { + "date +%s" | getline time; + "hostname" | getline host; + "uptime" | getline load; + sub(/.* /, "", load); + while ("df" | getline) { + if ($1 ~ "^/dev/" || $1 == "zroot") + stor += $4; + } +} + +NR == 2 { + split($0, keys); + for (key in keys) + if (keys[key] ~ /[0-9]$/) + disks[i++] = keys[key]; +} + +NR >= 4 { + for (i = 1 + split($0, values); i > 0; i--) + table[keys[i]] = values[i]; + mem = table[mem]; + cpu = 100 - table['id']; + load = load; + diskio = table[bi] + table[bo]; + for (disk in disks) + diskio += table[disks[disk]]; + print(host " " time " " mem " " cpu " " load " " diskio " " stor); + fflush(); +}' + +awk_plot='# -v STEP +BEGIN { + fields[2] = "time" + fields[3] = "mem" + fields[4] = "cpu" + fields[5] = "load" + fields[6] = "diskio" + fields[7] = "storage" +} + +{ + nfields = split($0, line, " ") + for (i = 1; i <= nfields; i++) + data[i"]["NR] = line[i] +} + +END { + # find the alignment of the data timestamps (phase) + for (i = 1; i <= NR; i++) + start += data[2"]["i] % STEP + start = int(start / NR + STEP / 2) + start += data[2"]["1] - data[2"]["1] % STEP + for (i = 3; i <= nfields; i++) { + offset = start +# printf "%s", fields[i] + for (j = 1; j <= NR; j++) { + while (data[2"]["j] > offset) { + printf " ?" + offset += STEP + } + offset += STEP + printf " %s", data[i"]["j] + } + print "" + } +}' + +case $1 in +(record) + [ $# -ne 2 ] && exec "$0" + case "$(uname)" in + (Linux ) mem=free bi=bi bo=bo ;; + (*BSD ) mem=fre bi='' bo='' ;; + (Solaris) mem=free bi='' bo='' ;; + (SunOS ) mem=free bi='' bo='' ;; + esac + vmstat "$2" | + awk -v mem="$mem" -v bi="$bi" -v bo="$bo" "$awk_record" >> "$DATA" + ;; +(plot) + [ $# -ne 2 ] && [ $# -ne 3 ] && exec "$0" + tail -n 80 "$DATA" | + awk -v STEP="${2:?argument missing} step}" "$awk_plot" | + plot -h "${3:-10}" + ;; +(*) + printf 'monit record delay | plot delay\n' + ;; +esac diff --git a/bin/monitor b/bin/monitor @@ -1,53 +0,0 @@ -#!/bin/sh -# ressources monitoring script - -# It will produce one line with the following values, separated with a space: -# -# 1. hostname 5. load average (1 minute) -# 2. unix time 6. disk I/O -# 3. free memory 7. total free storage -# 4. cpu usage - -case "$(uname)" in -(Linux ) mem=free bi=bi bo=bo ;; -(*BSD ) mem=fre bi='' bo='' ;; -(Solaris) mem=free bi='' bo='' ;; -(SunOS ) mem=free bi='' bo='' ;; -esac - -awk -v mem="$mem" -v bi="$bi" -v bo="$bo" ' - -BEGIN { - "date +%s" | getline time; - "hostname" | getline host; - "uptime" | getline load; - - sub(/.* /, "", load); - - while ("df" | getline) - if ($1 ~ "^/dev/" || $1 == "zroot") stor += $4; -} - -NR == 2 { - split($0, keys); - - for (key in keys) - if (keys[key] ~ /[0-9]$/) - disks[i++] = keys[key]; -} - -NR >= 4 { - for (i = 1 + split($0, values); i > 0; i--) - table[keys[i]] = values[i]; - - mem = table[mem]; - cpu = 100 - table['id']; - load = load; - diskio = table[bi] + table[bo]; - - for (disk in disks) - diskio += table[disks[disk]]; - - print(host " " time " " mem " " cpu " " load " " diskio " " stor); - fflush(); -}' diff --git a/bin/monitor-plot b/bin/monitor-plot @@ -1,47 +0,0 @@ -#!/bin/sh -# read the data from stdin and fills missing entries with "?" - -awk -v STEP="${1:?argument missing} step}" ' - -BEGIN { - fields[2] = "time" - fields[3] = "mem" - fields[4] = "cpu" - fields[5] = "load" - fields[6] = "diskio" - fields[7] = "storage" -} - -{ - nfields = split($0, line, " ") - - for (i = 1; i <= nfields; i++) - data[i"]["NR] = line[i] -} - -END { - # find the alignment of the data timestamps (phase) - for (i = 1; i <= NR; i++) - start += data[2"]["i] % STEP - start = int(start / NR + STEP / 2) - start += data[2"]["1] - data[2"]["1] % STEP - - for (i = 3; i <= nfields; i++) { - offset = start - printf "%s", fields[i] - for (j = 1; j <= NR; j++) { - while (data[2"]["j] > offset) { - printf " ?" - offset += STEP - } - offset += STEP - printf " %s", data[i"]["j] - } - print "" - } -}' | while read field line -do - printf '\n[ %s ] \n\n' "$field" - printf %s "$line" | plot -h 20 - printf '\n' -done diff --git a/bin/doc-rfc b/bin/rfc diff --git a/bin/www b/bin/www @@ -1,9 +1,7 @@ #!/bin/sh -# fast static website generator using POSIX sh, cat, printf, find, and sed - - -LAYOUT=$(cat layout.html) || exit 1 +# fast static website generator using POSIX sh, printf, find, and sed +LAYOUT=$(sed '' layout.html) || exit 1 #1 directory processed #2 content of the layout to parse @@ -17,7 +15,7 @@ parse() set -- "$1" "${2#*{{}" case "${2%%\}\}*}" in - (*content*) [ -f "$1/index" ] && cat "$1/index" ;; + (*content*) [ -f "$1/index" ] && sed "$1/index" ;; (*title* ) printf %s "${1##*/}" ;; (*root* ) root "$1" ;; (*nav* ) nav "$1" ;; @@ -27,13 +25,11 @@ parse() parse "$1" "${2#*\}\}}" } - root() { printf %s/ "$1" | sed 's|[^/]||g; s|.|../|g; s|/$||; s|^$|.|' } - nav() { find -L . -mindepth 1 -maxdepth 1 -type d ! -name '.*' | sort | @@ -42,7 +38,6 @@ nav() done } - list() { [ "$1" != . ] && find -L "$1" -mindepth 1 -maxdepth 1 ! -name 'index*' | @@ -54,7 +49,6 @@ list() done } - for dir in "$@" do parse "$dir" "$LAYOUT" > "$dir/index.html" done