From e60776a499ab93b6e2be70eb0e43338469f1c241 Mon Sep 17 00:00:00 2001 From: noerw Date: Sun, 13 Aug 2017 16:11:02 +0200 Subject: [PATCH] add monitor docker container --- tools/monitor/Dockerfile | 28 ++++++++++++++++++++++++++++ tools/monitor/README.md | 25 +++++++++++++++++++++++++ tools/monitor/analyze.R | 6 ++++++ tools/monitor/crontab | 1 + tools/monitor/docker-entry.sh | 5 +++++ tools/monitor/osem-monitor.R | 12 ++++++++++++ 6 files changed, 77 insertions(+) create mode 100644 tools/monitor/Dockerfile create mode 100644 tools/monitor/README.md create mode 100644 tools/monitor/analyze.R create mode 100644 tools/monitor/crontab create mode 100644 tools/monitor/docker-entry.sh create mode 100644 tools/monitor/osem-monitor.R diff --git a/tools/monitor/Dockerfile b/tools/monitor/Dockerfile new file mode 100644 index 0000000..11b0610 --- /dev/null +++ b/tools/monitor/Dockerfile @@ -0,0 +1,28 @@ +FROM r-base + +RUN apt-get update && \ + apt-get install -y libssl-dev libcurl3-dev cron + +# install dependencies +RUN Rscript -e 'install.packages("httr")' +RUN Rscript -e 'install.packages("dplyr")' +RUN Rscript -e 'install.packages("magrittr")' +#RUN Rscript -e 'install.packages("readr")' + +# install opensensemap package +RUN Rscript -e 'install.packages("devtools")' +RUN Rscript -e 'devtools::install_github("noerw/opensensmapR")' + +COPY docker-entry.sh /docker-entry.sh +RUN chmod 0755 /docker-entry.sh + +# install crontab +COPY crontab /crontab +RUN /usr/bin/crontab /crontab + + +# install script +COPY osem-monitor.R /script/osem-monitor.R +WORKDIR /script/data + +CMD ["/docker-entry.sh"] diff --git a/tools/monitor/README.md b/tools/monitor/README.md new file mode 100644 index 0000000..f8f3303 --- /dev/null +++ b/tools/monitor/README.md @@ -0,0 +1,25 @@ +# osem-monitor + +Get the state of sensors and measurement counts for later analysis every 10 minutes. +The dataframe will reside in `./data/.RData`. + +Further analysis can be done with the script `analyze.R`. + +## docker image +```bash +# build +docker build --tag osem-monitor . + +# run +docker run -v $(pwd)/data:/script/data osem-monitor +``` + +## run manually + +```bash +# install dependencies once +Rscript -e 'install.packages(c("dplyr", "magrittr", "devtools"))' +Rscript -e 'devtools::install_github("noerw/opensensmapR")' + +Rscript --save --restore osem-monitor.R +``` diff --git a/tools/monitor/analyze.R b/tools/monitor/analyze.R new file mode 100644 index 0000000..2f1d65f --- /dev/null +++ b/tools/monitor/analyze.R @@ -0,0 +1,6 @@ +# create a working copy, so we cant accidentally loose all data +ts = osem_counts_ts + +# compute our own measurements per minute, to check against the provided computation +ts$measurement_diff = c(diff(ts$measurements), 0) +ts$measurement_minute = ts$measurement_diff / c(as.numeric(diff(ts$time), unit = 'mins'), 0) diff --git a/tools/monitor/crontab b/tools/monitor/crontab new file mode 100644 index 0000000..dd7ed62 --- /dev/null +++ b/tools/monitor/crontab @@ -0,0 +1 @@ +*/10 * * * * cd /script/data && /usr/bin/Rscript --save --restore ../osem-monitor.R > rscript.log diff --git a/tools/monitor/docker-entry.sh b/tools/monitor/docker-entry.sh new file mode 100644 index 0000000..c11598a --- /dev/null +++ b/tools/monitor/docker-entry.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +# start cron +cron -f -l 0 + diff --git a/tools/monitor/osem-monitor.R b/tools/monitor/osem-monitor.R new file mode 100644 index 0000000..513d6b8 --- /dev/null +++ b/tools/monitor/osem-monitor.R @@ -0,0 +1,12 @@ +library(dplyr) +library(magrittr) +library(opensensemap) + +if (is.null(as.list(environment())$osem_counts_ts)) + osem_counts_ts = data.frame() + +osem_counts_ts = osem_counts() %>% + list(time = Sys.time()) %>% + as.data.frame() %>% + dplyr::bind_rows(osem_counts_ts) +