Skip to content

Instantly share code, notes, and snippets.

@mustafaascha
Created November 27, 2018 01:48
Show Gist options
  • Save mustafaascha/013cb4803e4ff36e7076a54e121bab1e to your computer and use it in GitHub Desktop.
Save mustafaascha/013cb4803e4ff36e7076a54e121bab1e to your computer and use it in GitHub Desktop.
Get a tidy representation of disk usage
library(tidyverse)
rm_rownames <- function(z) {
rownames(z) <- NULL
z
}
sz_message <- function(sz, p_nm, ttl, bs) {
p_mn <- round(mean(sz[[p_nm]], na.rm = T))
message(
paste(
"It appears you're using ",
p_mn, "% of a total ", ttl, bs, " of dev-level blocks",
sep = ""
)
)
}
# test <-
# system("sudo -kS df -h /", input = rstudioapi::askForPassword())
tidy_sys_df <- function(blocksize) {
stopifnot(blocksize %in% c("K", "M", "G", "T", "P", "E", "Z", "Y"))
the_command <- paste("df -B", blocksize, sep = "")
p_bs <- function(the_x) paste(tolower(blocksize), the_x, sep = "_")
bs_nm <- p_bs("blocks")
pu_nm <- p_bs("used")
system(the_command, intern = T) %>%
map(~ strsplit(.x, "\\ {1,}")) %>%
(function(a) {
nms <- a[[1]][[1]]
#nms <- nms[-length(nms)]
a[1] <- NULL
#list(nm = nms, rws = a)
# THERES A SAFER WAY TO SELECT FOR THE CORRECT NAME
map(a, ~ set_names(flatten_chr(.x), nm = nms[-7])) %>%
reduce(rbind) %>%
rm_rownames()
}) %>%
as.data.frame(stringsAsFactors = FALSE) %>%
rename_all(tolower) %>%
rename_all(
function(x) {
reduce2(
c("%", "^[^a-z]", "[^a-z]", "^used$"),
c("_perc", "", "_", pu_nm),
function(a, b, d) gsub(b, d, a),
.init = x
)
}) %>%
mutate(
u_units = str_extract(!!pu_nm, "[A-Za-z]"),
bs_units = str_extract(!!bs_nm, "[A-Za-z]"),
a_units = str_extract(available, "[A-Za-z]")
) %>%
modify_at(c(bs_nm, pu_nm, "available", "use_perc"), ~ as.numeric(gsub("[^0-9]", "", .x))) %>%
filter(grepl("dev", filesystem)) %>%
(function(a) {
message(paste("Using", bs_nm, "as the blocksize variable name and", pu_nm, "as the used blocksize name"))
sz_message(a, "use_perc", round(sum(a[["available"]]) / mean(a[["use_perc"]] / 100)), blocksize)
a
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment