linux 服务器之查看磁盘使用情况
使用接口statfs
函數(shù)接口:
int statfs(const charpath, struct statfsbuf);
 ?參數(shù):
 ??path: 位于需要查詢信息的文件系統(tǒng)的文件路徑
 ??buf: statfs結(jié)構(gòu)體類型的指針變量,用于存儲(chǔ)文件系統(tǒng)的相關(guān)信息
?statfs 結(jié)構(gòu)體:
struct statfs{
 ?? ?long f_type; ? ? //文件系統(tǒng)的類型
 ?? ?long f_bsize; ? //經(jīng)優(yōu)化后的傳輸塊的大小
 ?? ?long f_blocks; ?//文件系統(tǒng)數(shù)據(jù)塊總數(shù)
 ?? ?long f_bfree; ? ?//可用塊數(shù)
 ?? ?long f_bavail; ? //普通用戶能夠獲得的塊數(shù)
 ?? ?long f_files; ? ? ?//文件結(jié)點(diǎn)總數(shù)
 ?? ?long f_ffree; ? ? //可用文件結(jié)點(diǎn)數(shù)
 ?? ?fisd_t f_fsid; ? ? //文件系統(tǒng)標(biāo)識(shí)
 ?? ?long f_namelen; ?//文件名的最大長(zhǎng)度
 }
 ?
Tips:
f_bfree VS f_bavail 區(qū)別:
f_bfree? 是 硬盤所有剩余空間
f_bavail 是?非root用戶剩余空間,?一般ext3文件系統(tǒng)會(huì)給root留5%的獨(dú)享空間
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;//磁盤整體空間字節(jié)數(shù), f_blocks * f_bsizeif (NULL != total)*total = (zbx_uint64_t)s.f_blocks * s.ZBX_BSIZE;// 用戶級(jí)空閑空間的大小, 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平臺(tái)下使用 df -h / 驗(yàn)證磁盤實(shí)際使用情況
需要注意的是, df 命令獲得的是整數(shù)百分比,沒(méi)有小數(shù),使用的是進(jìn)一法,而不是四舍五入法。
總結(jié)
以上是生活随笔為你收集整理的linux 服务器之查看磁盘使用情况的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: PMP敏捷图表之价值流程图
 - 下一篇: linux服务器之查看内存使用情况