-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices.sql
64 lines (56 loc) · 1.77 KB
/
devices.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---------------------------------------------------
-- disk free
---------------------------------------------------
CREATE OR REPLACE FUNCTION devices(
OUT dev integer, OUT filesystem text, OUT fstype text, OUT mountpoint text,
OUT size numeric, OUT used numeric, OUT avail numeric, OUT "%use" double precision, OUT options text[])
RETURNS SETOF record
LANGUAGE plperlu
COST 1000 ROWS 10
AS $function$
# Get UNIX file system information
# this uses UNIX df(1) and mount(1) commands
my %d=();
# reported by df(1)
my $df=`df -B1 -a -P`;my @df=split(/[\n\r]+/,$df); shift @df;
for my $l (@df) {
my @a=split(/\s+/,$l); my $i=$a[0];
$d{$i}={
'filesystem'=>$i,'size'=>$a[1],'used'=>$a[2],
'avail'=>$a[3],'%use'=>$a[4],'mountpoint'=>$a[5]
};
if( $d{$i}{'size'}eq'-') { $d{$i}{'size'}=undef; }
if( $d{$i}{'used'}eq'-') { $d{$i}{'used'}=undef; }
if( $d{$i}{'avail'}eq'-') { $d{$i}{'avail'}=undef; }
if( $d{$i}{'%use'}eq'-') { $d{$i}{'%use'}=undef; }
else { $d{$i}{'%use'}=~s/%//; }
my @stat=stat($d{$i}{'mountpoint'});
$d{$i}{'dev'}=$stat[0];
}
# reported by mount(1)
my $mt=`mount`; my @mt=split(/[\n\r]+/,$mt);
for my $l (@mt) {
my @a=split(/\s+/,$l);
$d{$a[0]}{'fstype'}=$a[4];
$d{$a[0]}{'options'}=$a[5];
$d{$a[0]}{'options'}=~y/\(\)/\{\}/;
}
for my $i (keys(%d)) { return_next($d{$i}); }
return undef;
$function$;
COMMENT ON FUNCTION devices() IS 'Get disk device information';
REVOKE ALL ON FUNCTION devices() FROM PUBLIC;
CREATE OR REPLACE VIEW devices AS
SELECT dev,
filesystem,
fstype,
mountpoint,
size,
used,
avail,
"%use",
options
FROM datalink.devices()
WHERE fstype IS NOT NULL and dev is not null;
COMMENT ON VIEW devices IS 'Get disk device information';
GRANT SELECT ON devices TO PUBLIC;