linux 服务器之查看磁盘使用情况
使用接口statfs
函數接口:
int statfs(const charpath, struct statfsbuf);
?參數:
??path: 位于需要查詢信息的文件系統的文件路徑
??buf: statfs結構體類型的指針變量,用于存儲文件系統的相關信息
?statfs 結構體:
struct statfs{
?? ?long f_type; ? ? //文件系統的類型
?? ?long f_bsize; ? //經優化后的傳輸塊的大小
?? ?long f_blocks; ?//文件系統數據塊總數
?? ?long f_bfree; ? ?//可用塊數
?? ?long f_bavail; ? //普通用戶能夠獲得的塊數
?? ?long f_files; ? ? ?//文件結點總數
?? ?long f_ffree; ? ? //可用文件結點數
?? ?fisd_t f_fsid; ? ? //文件系統標識
?? ?long f_namelen; ?//文件名的最大長度
}
?
Tips:
f_bfree VS f_bavail 區別:
f_bfree? 是 硬盤所有剩余空間
f_bavail 是?非root用戶剩余空間,?一般ext3文件系統會給root留5%的獨享空間
zabbix 源碼:
static int get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free,zbx_uint64_t *used, double *pfree, double *pused, char **error) { #ifdef HAVE_SYS_STATVFS_H # define ZBX_STATFS statvfs # define ZBX_BSIZE f_frsize #else # define ZBX_STATFS statfs # define ZBX_BSIZE f_bsize #endifstruct ZBX_STATFS s;if (NULL == fs || '\0' == *fs){*error = zbx_strdup(NULL, "Filesystem name cannot be empty.");zabbix_log(LOG_LEVEL_DEBUG,"%s failed with error: %s",__func__, *error);return SYSINFO_RET_FAIL;}if (0 != ZBX_STATFS(fs, &s)){*error = zbx_dsprintf(NULL, "Cannot obtain filesystem information: %s", zbx_strerror(errno));zabbix_log(LOG_LEVEL_DEBUG,"%s failed with error: %s",__func__, *error);return SYSINFO_RET_FAIL;}/* Available space could be negative (top bit set) if we hit disk space *//* reserved for non-privileged users. Treat it as 0. */if (0 != ZBX_IS_TOP_BIT_SET(s.f_bavail))s.f_bavail = 0;//磁盤整體空間字節數, f_blocks * f_bsizeif (NULL != total)*total = (zbx_uint64_t)s.f_blocks * s.ZBX_BSIZE;// 用戶級空閑空間的大小, f_bavail * f_bsizeif (NULL != free)*free = (zbx_uint64_t)s.f_bavail * s.ZBX_BSIZE;//已使用空間大小: (f_blocks - f_bfree)* f_bsizeif (NULL != used)*used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.ZBX_BSIZE;//可用百分比if (NULL != pfree){if (0 != s.f_blocks - s.f_bfree + s.f_bavail)*pfree = (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail);else*pfree = 0;}//已用百分比if (NULL != pused){if (0 != s.f_blocks - s.f_bfree + s.f_bavail)*pused = 100.0 - (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail);else*pused = 0;}return SYSINFO_RET_OK; }Linux平臺下使用 df -h / 驗證磁盤實際使用情況
需要注意的是, df 命令獲得的是整數百分比,沒有小數,使用的是進一法,而不是四舍五入法。
總結
以上是生活随笔為你收集整理的linux 服务器之查看磁盘使用情况的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PMP敏捷图表之价值流程图
- 下一篇: linux服务器之查看内存使用情况