https://github.com/ColinFay/dockerstats
R Wrapper Around 'docker stats'
https://github.com/ColinFay/dockerstats
Last synced: 4 months ago
JSON representation
R Wrapper Around 'docker stats'
- Host: GitHub
- URL: https://github.com/ColinFay/dockerstats
- Owner: ColinFay
- License: other
- Created: 2020-04-09T13:06:22.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-15T21:37:09.000Z (almost 5 years ago)
- Last Synced: 2024-04-25T02:42:09.200Z (12 months ago)
- Language: R
- Size: 274 KB
- Stars: 7
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - ColinFay/dockerstats - R Wrapper Around 'docker stats' (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# dockerstats
`{dockerstats}` is a small wrapper around `docker stats` that returns the output of this command as an R data.frame.
Note that this package calls `system("docker stats")` so you should be able to do this from your R command line. I'll probably refactor that at some point.
## Installation
You can install the released version of `{dockerstats}` from GitHub with:
``` r
remotes::install_github("ColinFay/dockerstats")
```## How to
```{r}
library(dockerstats)
```### dockerstats()
By default, `dockerstats()` returns the stats for running containers.
```{r}
dockerstats()
```You can return stats for all containers (not just running)
```{r}
dockerstats(all = TRUE)
```Or from a subset of containers:
```{r}
dockerstats("mongo", "proxy")
```The `extra` param is used to add extra information to the recording, which can be usefull is you want to tag the specific recording.
For example, here we mimic a connection on the `hexmake` container
```{r echo = FALSE}
system("docker run --name hexmake --rm -p 2811:80 colinfay/hexmake", wait = FALSE)
Sys.sleep(5)
``````{r eval = FALSE}
system("docker run --name hexmake --rm -p 2811:80 colinfay/hexmake", wait = FALSE)```
```{r, eval = FALSE}
library(crrri)
chrome <- Chrome$new(
bin = pagedown::find_chrome(),
debug_port = httpuv::randomPort()
)client <- chrome$connect(callback = function(client) {
Page <- client$Page
Page$navigate(url = "http://localhost:2811")
print({
dockerstats::dockerstats("hexmake", extra = "Connection via chrome")
})
})chrome$close()
```### dockerstats_recurse
`dockerstats_recurse()` is a wrapper around `dockerstats()` that runs every `every` seconds, calling the `callback` function everytime a loop is completed.
By default, the callback is `print`, but you can define your own. For example, this function will run the `dockerstats()` fun every 2 seconds and save it to a file.
```{r eval = FALSE}
dockerstats_recurse(
"hexmake",
callback = function(res){
print(
paste("Mem usage: ", res$MemUsage)
)
write.table(
res,
"dockerstats.csv",
append = TRUE,
col.names = FALSE,
row.names = FALSE,
sep = ","
)
}
)
```As this is a pretty common use-case, a wrapper for this is implemented:
```{r eval = FALSE}
dockerstats_recurse(
"hexmake",
callback = append_csv("dockerstats.csv", print = TRUE)
)
```Kill the container once done
```{r}
system("docker kill hexmake")
```## Byte conversions
To handle the `MemUsage` columns, expressed as `byte`, `{dockerstats}` provides a series of functions to convert to byte, kib, mib or gib.
The unit is kept in `attr("units")` of the result
```{r}
dock_stats <- dockerstats()
dock_stats$MemUsag
# Convert to kib
to_kib(dock_stats$MemUsag)
```## How columns are transformed
+ `CPUPerc` and `MemPerc` are stripped from the trailing `%`, and are turned into numeric
+ `MEM USAGE / LIMIT` is splitted into two columns, `MemUsage` and `MemLimit
+ `NET I/O` and `BLOCK I/O` are splitted into two columns, respectively `NetI` & `NetO`, and `BlockI` & `BlockO`
## Manipulate columns expressed in bite size
You can call `as_fs_byte()` from the `{fs}` package to manipulate the columns which are expressed in bytes.
```{r}
dock_stats <- dockerstats()dock_stats$MemUsage <- to_kib(dock_stats$MemUsag)
``````{r}
library(ggplot2)
ggplot(
dock_stats,
aes(
reorder(Name, MemUsage),
MemUsage
)
) +
geom_col() +
scale_y_continuous(labels = scales::label_bytes(units = "kiB")) +
coord_flip() +
labs(
title = "MemUsage of running containers",
y = "MemUsage in kiB",
x = "Containers"
)
```