简体   繁体   English

如何导出每日磁盘空间使用情况?

[英]How to export daily disk space usage?

I am new to scripting. 我是脚本新手。

Is it possible to export the disk space usage of a linux server to an excel shell(.csv) sheet daily ? 是否可以每天将linux服务器的磁盘空间使用情况导出到excel shell(.csv)工作表? If so, what will be the scripting for that? 如果是这样,那么脚本将是什么?

Using shell sctript you can write it as below. 使用shell sctript,您可以编写如下。 And then you can schedule your script by using crontabs. 然后,您可以使用crontabs安排脚本。

Script:- 脚本:-

#!/bin/bash

DDATE=$(date '+%Y-%m-%d')
{
TIME=$(date)
DISK=$(df -h)

echo $DDATE

cat <<-EOF

>>>>Disk space<<<<
$DISK
EOF

wait

} > /tmp/output.csv
exit;

Use awk to parse df result: 使用awk解析df结果:

Assuming ; 假设; is your csv separator, then: 是您的csv分隔符,然后:

df -h | awk 'FNR == 2 {print $2";"$3";"$4}'

gives: 给出:

24G;5.4G;18G

In a full script with the date: 在带有日期的完整脚本中:

#!/bin/bash

SEPARATOR=","
SIZES=`df -h | awk -v SEP="$SEPARATOR" 'FNR == 2 {print SEP$2SEP$3SEP$4}'`
echo `date +%Z-%Y-%m-%d_%H-%M-%S`"$SIZES" >> test.csv

如果您需要更高级的每日统计数据和历史记录,可以在crontab中使用诸如http://diskreport.net之类的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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