1 /*
2  * libstatgrab
3  * https://libstatgrab.org
4  * Copyright (C) 2003-2004 Peter Saunders
5  * Copyright (C) 2003-2019 Tim Bishop
6  * Copyright (C) 2003-2013 Adam Sampson
7  * Copyright (C) 2012-2019 Jens Rehsack
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 /* A very basic example of how to get the filesystem statistics from the
26  * system and display them.
27  */
28 
29 #include <stdio.h>
30 #include <statgrab.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 #include "helpers.h"
35 
36 #ifdef HAVE_LOG4CPLUS_INITIALIZE
37 static void *l4cplus_initializer;
38 
39 static void
cleanup_logging(void)40 cleanup_logging(void)
41 {
42 	log4cplus_deinitialize(l4cplus_initializer);
43 }
44 #endif
45 
main(int argc,char ** argv)46 int main(int argc, char **argv){
47 	sg_fs_stats *fs_stats;
48 	size_t fs_size;
49 
50 #ifdef HAVE_LOG4CPLUS_INITIALIZE
51 	l4cplus_initializer = log4cplus_initialize();
52 	atexit((void (*)(void))cleanup_logging);
53 #endif
54 
55 	/* Initialise helper - e.g. logging, if any */
56 	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);
57 
58 	/* Initialise statgrab */
59 	sg_init(1);
60 
61 	/* Drop setuid/setgid privileges. */
62 	if (sg_drop_privileges() != 0) {
63 		perror("Error. Failed to drop privileges");
64 		return 1;
65 	}
66 
67 	fs_stats = sg_get_fs_stats(&fs_size);
68 	if(fs_stats == NULL)
69 		sg_die("Failed to get file systems snapshot", 1);
70 
71 	printf( "%-16s %-24s %-8s %16s %16s %16s %7s %7s %7s %7s %9s %9s %7s %7s %7s %7s\n",
72 		"device", "mountpt", "fstype", "size", "used", "avail",
73 		"i-total", "i-used", "i-free", "i-avail",
74 		"io size", "block size",
75 		"b-total", "b-used", "b-free", "b-avail");
76 
77 	for( ; fs_size > 0 ; --fs_size, ++fs_stats ) {
78 		printf( "%-16s %-24s %-8s %16llu %16llu %16llu %7llu %7llu %7llu %7llu %9llu %9llu %7llu %7llu %7llu %7llu\n",
79 			fs_stats->device_name, fs_stats->mnt_point, fs_stats->fs_type,
80 			fs_stats->size, fs_stats->used, fs_stats->avail,
81 			fs_stats->total_inodes, fs_stats->used_inodes, fs_stats->free_inodes, fs_stats->avail_inodes,
82 			fs_stats->io_size, fs_stats->block_size,
83 			fs_stats->total_blocks, fs_stats->used_blocks, fs_stats->free_blocks, fs_stats->avail_blocks);
84 	}
85 
86 	return 0;
87 }
88