简体   繁体   中英

R CPU Utilisation Heat map in data center

I have readings from 100 servers in the data center. The readings are in a data frame format of time having 3 columns Time , host name , CPU Utilization . The readings are every 10 mins generated by a monitoring system. I need to plot a heat map of CPU utilization, with time on X axis and % of servers on Y axis with CPU utilization range in Heat map.

For example : If total number of servers is 5 . The input data is as follows

Time            CPU Hostname
1/25/2015 10:15  19%    H1
1/25/2015 10:15  90%    H2
1/25/2015 10:15  90%    H3
1/25/2015 10:15  50%    H4
1/25/2015 10:15  25%    H5
1/25/2015 10:25  30%    H1
1/25/2015 10:25  85%    H2
1/25/2015 10:25  30%    H3
1/25/2015 10:25  21%    H4
1/25/2015 10:25  21%    H5

The output required is a stacked chart to depict the following figures in a heat map.

For example at 10:15 there are 2 servers in range of 80-100% utilization and hence value is 40%

Range   10:15   10:25
0-20    20%      0%
20-40   20%      80%
40-60   20%      0%
60-80   0%       0%
80-100  40%      20%

Need help on the functions in R to plot this kind of heat map. Have tried to use xts but I am not clear on this use case of how to apply the xts package.

You just need to:

  • cut values into the groups you need
  • find the % per group
  • expand out missing entries
  • use geom_tile for your heatmap

Many of the components of following code are in many SO posts:

library(dplyr)
library(ggplot2)
library(tidyr)
library(scales)

dat <- read.table(text="Time,CPU,Hostname
1/25/2015 10:15,19%,H1
1/25/2015 10:15,90%,H2
1/25/2015 10:15,90%,H3
1/25/2015 10:15,50%,H4
1/25/2015 10:15,25%,H5
1/25/2015 10:25,30%,H1
1/25/2015 10:25,85%,H2
1/25/2015 10:25,30%,H3
1/25/2015 10:25,21%,H4
1/25/2015 10:25,21%,H5", header=TRUE, sep=",", stringsAs=FALSE)


total_hosts <-length(unique(dat$Hostname))

dat %>%
  mutate(Time=as.POSIXct(Time, format="%m/%d/%Y %H:%M"),
         Day=format(Time, format="%Y-%m-%d"),
         HM=format(Time, format="%H:%M"),
         CPU=as.numeric(gsub("%", "", CPU)),
         `CPU Range`=as.character(cut(CPU, 
                                breaks=c(0,20,40,60,80,100), 
                                labels=c("0-20", "20-40", "40-60", 
                                         "60-80", "80-100")))) %>%
  group_by(Day, `CPU Range`, HM) %>%
  summarise(Pct=n()/total_hosts) %>%
  merge(expand(., `CPU Range`, HM, Day), all.y=TRUE) -> dat

gg <- ggplot(dat, aes(x=HM, y=`CPU Range`))
gg <- gg + geom_tile(aes(fill=Pct), color="#7f7f7f")
gg <- gg + scale_fill_distiller(palette="RdPu", na.value="white", 
                                label=percent, name="% Hosts")
gg <- gg + coord_equal()
gg <- gg + labs(x=NULL)
gg <- gg + theme_bw()
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.grid=element_blank())
gg

在此处输入图片说明

I left the Day in the data frame in case you want/need to facet_wrap by it or aggregate by it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM