使用Linux中仅CLI的工具生成磁盘使用情况图/图表 [英] Generate disk usage graphs/charts with CLI only tools in Linux

查看:89
本文介绍了使用Linux中仅CLI的工具生成磁盘使用情况图/图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中有人问了一些方法显示Linux中的磁盘使用情况.我想在cli-path的基础上再迈出一步...一个shell脚本如何从类似上一个问题的合理答案中获取输出并从中生成图形/图表(以png格式输出)文件或其他内容)?在常规问题中可能要问的代码太多了,但是我猜是某人已经在某个地方放了一个内衬纸...

In this question someone asked for ways to display disk usage in Linux. I'd like to take this one step further down the cli-path... how about a shell script that takes the output from something like a reasonable answer to the previous question and generates a graph/chart from it (output in a png file or something)? This may be a bit too much code to ask for in a regular question, but my guess is that someone already has a oneliner laying around somewhere...

推荐答案

我建议 munin .它专为此类事情而设计-绘制CPU使用情况,内存使用情况,磁盘使用情况等信息.有点像MRTG(但MRTG的主要目的是绘制路由器流量,绘制除带宽以外的任何图形都非常骇人听闻)

I would recommend munin. It is designed for exactly this sort of thing - graphing CPU usage, memory usage, disc-usage and such. sort of like MRTG (but MRTG is primarily aimed at graphing router's traffic, graphing anything but bandwidth with it is very hackish)

编写Munin插件非常容易(这是项目目标之一).它们几乎可以用任何东西(shell脚本,perl/python/ruby​​/etc,C,可以执行并产生输出的任何东西)编写.插件的输出格式基本上是disc1usage.value 1234.而且调试插件非常容易(与MRTG相比)

Writing Munin plugins is very easy (it was one of the projects goals). They can be written in almost anything (shell script, perl/python/ruby/etc, C, anything that can be execute and produce an output). The plugin output format is basically disc1usage.value 1234. And debugging the plugins is very easy (compared to MRTG)

我已经在笔记本电脑上对其进行了设置,以监视光盘使用情况,带宽使用情况(通过从ISP控制面板中提取数据,并绘制出我的两个下载"bin",上传文件和新闻组的使用情况),平均负载和流程.一旦我安装了它(目前在OS X上有点困难,但是在Linux/FreeBSD上却很琐碎),我在几分钟内就编写了一个插件,并且第一次就可以使用了!

I've set it up on my laptop to monitor disc-usage, bandwidth usage (by pulling data from my ISP's control panel, it graphs my two download "bins", uploads and newsgroup usage), load average and number of processes. Once I got it installed (currently slightly difficult on OS X, but it's trivial on Linux/FreeBSD), I had written a plugin in a few minutes, and it worked, first time!

我将描述它的设置方式,但是munin网站会做得比我做得好得多!

I would describe how it's setup, but the munin site will do that far better than I could!

有一个示例安装此处

有些替代品是nagios和仙人掌.您也可以使用rrdtool编写类似的内容. Munin,MRTG和Cacti基本上都是基于此绘图工具而更易于使用的系统.

Some alternatives are nagios and cacti. You could also write something similar using rrdtool. Munin, MRTG and Cacti are basically all far-nicer-to-use systems based around this graphing tool.

如果您想要真正非常简单的东西,则可以做..

If you want something really, really simple, you could do..

import os
import time
while True:
    disc_usage = os.system("df -h / | awk '{print $3}'")
    log = open("mylog.txt")
    log.write(disc_usage + "\n")
    log.close()
    time.sleep(60*5)

然后..

f = open("mylog.txt")
lines = f.readlines()

# Convert each line to a float number
lines = [float(cur_line) for cur_line in lines]

# Get the biggest and smallest
biggest = max(lines)
smallest = min(lines)

for cur_line in lines:
    base = (cur_line - smallest) + 1 # make lowest value 1
    normalised = base / (biggest - smallest) # normalise value between 0 and 1
    line_length = int(round(normalised * 28)) # make a graph between 0 and 28 characters wide
    print "#" * line_length

这将为光盘使用情况制作一个简单的ascii图.我真的不建议您使用类似这样的内容.为什么?日志文件将变得越来越大.图形将逐渐变慢. RRDTool使用滚动数据库系统来存储其数据,因此文件的大小永远不会超过50-100KB,并且由于文件的长度固定,因此可以始终以图形方式进行显示.

That'll make a simple ascii graph of the disc usage. I really really don't recommend you use something like this. Why? The log file will get bigger, and bigger, and bigger. The graph will get progressively slower to graph. RRDTool uses a rolling-database system to store it's data, so the file will never get bigger than about 50-100KB, and it's consistently quick to graph as the file is a fixed length.

简而言之.如果您希望某些东西可以轻松绘制几乎所有内容,请使用 munin .如果您想要更小巧且功能齐全的产品,请使用RRDTool编写一些内容.

In short. If you want something to easily graph almost anything, use munin. If you want something smaller and self-contained, write something with RRDTool.

这篇关于使用Linux中仅CLI的工具生成磁盘使用情况图/图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆