前言
自从成为服务器管理者以来,一直监测服务器quota使用情况,但一直是用表格的形式,现在想把其可视化了。更多知识分享请到 https://zouhua.top/。
表格式的Quota结果
一眼看去极其不直观,也不美观且特别繁琐,每次都要使用下面的Linux命令手动处理,故抛弃它
df -h /home/
df -h /disk1/
图形化
结合上述获取quota的Linux command,再结合R脚本可视化结果
df -h /home/ > quota.tsv
df -h /disk/ >> quota.tsv
df -h /disk1/ >> quota.tsv
df -h /disk2/ >> quota.tsv
df -h /disk3/ >> quota.tsv
grep -v "Filesystem" quota.tsv | awk 'BEGIN{print"Size\tUsed\tAvailable\tDisk"}{print $2,$3,$4,$6}' | sed 's/T//g;s/G//g' > quota_final.tsv
Rscript quota_plot.R
- 可视化R脚本 quota_plot.R
library(ggplot2)
library(dplyr)
dat <- read.table("quota_final.tsv", header = T) %>%
mutate(Size=Used+Available)
plotdata <- dat %>% mutate(Disk=gsub("/", "", Disk)) %>%
group_by(Disk) %>%
mutate(Used_percentage=Used/Size,
Avail_percentage=Available/Size) %>%
dplyr::select(Disk, Used_percentage, Avail_percentage) %>%
tidyr::gather(key="Type", value="Quota", -Disk) %>%
mutate(Disk=factor(Disk, levels = c("home","disk","disk1","disk2","disk3"))) %>%
mutate(Type=factor(Type, levels = c("Used_percentage", "Avail_percentage"), labels = c("Used", "Available"))) %>%
mutate(Quota_Disk=ifelse(Disk=="home",
paste0(round(Quota*dat$Size[1], 1), "G"),
paste0(round(Quota*dat$Size[2], 1), "T")))
pl <- ggplot(plotdata, aes(x=Disk, y=Quota, fill=Type))+
geom_bar(position="stack", stat="identity")+
scale_fill_manual(values = c("red", "blue")) +
scale_y_continuous(breaks = seq(0, 1, 0.25),
labels = scales::percent,
limits = c(0, 1),
expand = c(0, 0)) +
geom_text(aes(label = paste(scales::percent(Quota), plotdata$Quota_Disk, sep = "\n")),
position = position_stack(vjust = .5),
size=5, color="white")+
labs(x = '', y = 'Quota (%)', fill = NULL)+
guides(fill = guide_legend(bycol = TRUE, override.aes = list(size = 6))) +
theme_bw()+
theme(axis.title.y = element_text(face = 'bold',color = 'black',size = 14),
axis.title.x = element_text(face = 'bold',color = 'black',size = 14,vjust = -1.2),
axis.text.y = element_text(face = 'bold',color = 'black',size = 10),
axis.text.x = element_text(face = 'bold',color = 'black',size = 12,
angle = 45,vjust = 0.5),
panel.grid = element_blank(),
legend.position = 'bottom',
#legend.direction = "horizontal",
legend.key.height = unit(0.6,'cm'),
legend.text = element_text(face = 'bold',color = 'black',size = 10))
ggsave("quota_stacked_barplot.pdf", pl, width = 5.5, height = 5, dpi = 600)
最后结果如下:
Notes: 后面可以加入时间戳等信息,这样每次生成的结果都能保存下来而不会被覆盖。
网友评论