# Monitor disk usage



## xy16644 (May 14, 2011)

Can anyone recommend a port that I can use/install that will allow me to monitor the size of the directories I choose by going to a web page?

Basically I would like to list the size of users home directories and/or mailbox sizes but I would like to view all this info on a web page.

The other thing I would like to do is montior the status of my disk mirror (RAID 1) which uses gmirror (in a web page too).

Any suggestions? :e


----------



## pbd (May 18, 2011)

In a nutshell:

- install Apache
- write a cgi script, something like:


```
#!/bin/sh

du -sh /home/*
du -sh /var/mail/*

gmirror status
```


----------



## mk (May 18, 2011)

I made one for myself.. quick and ugly. 
Take a notice of text/plain. If you use text/html the output of all commands is put on one line, no separation.

```
#!/bin/sh

echo Content-type: text/plain
echo
df -h
echo
service -e
echo
mount
echo                                                      
sockstat -l4c
```


----------



## SirDice (May 18, 2011)

Or use net-mgmt/mrtg to create nice graphs of the disk usage.


----------



## shitson (May 18, 2011)

Maybe Nagios?


----------



## xy16644 (May 20, 2011)

pbd said:
			
		

> In a nutshell:
> 
> - install Apache
> - write a cgi script, something like:
> ...



Thanks for that! I created it as follows:

```
#!/bin/sh

du -hs /home/*

gmirror status
```

When I run it manually, it runs fine. But since I have copied it into my cgi-bin directory, I get an Internal Server Error (when browsing it). Is there anything else I need to do?


----------



## xy16644 (May 20, 2011)

After a bit more digging around in the logs I think its a permission error:

```
alpha# tail -F /var/log/httpd-error.log
[Fri May 20 12:26:10 2011] [error] [client 192.168.0.10] du: /home/test/Maildir: Permission denied
[Fri May 20 12:26:10 2011] [error] [client 192.168.0.10] du: /home/test/.ssh: Permission denied
[Fri May 20 12:26:10 2011] [error] [client 192.168.0.10] du: /home/test/.gnupg: Permission denied
[Fri May 20 12:26:10 2011] [error] [client 192.168.0.10] malformed header from script. Bad header= 20K\t/home/copy: monitor.cgi
```

So the question is, how do I get this script to execute with the correct permissions?


----------



## SirDice (May 20, 2011)

The reason it's giving a 500 server error is because the script needs to return

```
200 OK
```
After that it should supply a Content-type: header so the browser knows what to do with it. Then you can "print" any information to the client.


----------



## xy16644 (May 20, 2011)

OK, so how do I get it to return a 200 OK? Sorry for the obvious questions but I have never used cgi scripts before...


----------



## SirDice (May 20, 2011)

```
#!/bin/sh

echo "200 OK"
echo
echo "Content-Type: text/plain"

du -hs /home/*

gmirror status
```


----------



## xy16644 (May 20, 2011)

SirDice said:
			
		

> ```
> #!/bin/sh
> 
> echo "200 OK"
> ...



Tried that but I still have the "Internal Server Error". When I check the httpd-error.log theres many access denied messages. How can this script execute successfully when it is being run as the www user?


----------



## DutchDaemon (May 20, 2011)

Quick and dirty: set up security/sudo so that the www user can perform this script (_and only this script_) as the root user and without a password.


----------



## xy16644 (May 20, 2011)

DutchDaemon said:
			
		

> Quick and dirty: set up security/sudo so that the www user can perform this script (_and only this script_) as the root user and without a password.



Great, I have sudo installed now. I have tried following some of the examples and added:

```
www localhost=NOPASSWD: /usr/local/www/apache22/cgi-bin/monitor.cgi
```

to the sudoers config file but no luck yet. I restarted Apache and also ran:

```
alpha# visudo -c
/usr/local/etc/sudoers: parsed OK
```

Still seem to be stuck with the dreaded "500 Internal Server error"!


----------



## DutchDaemon (May 21, 2011)

Is your general cgi setup (in Apache's configuration files) ok, and is the script executable? Does it have a proper hashbang? Stuff like that.


----------



## xy16644 (May 21, 2011)

DutchDaemon said:
			
		

> Is your general cgi setup (in Apache's configuration files) ok, and is the script executable? Does it have a proper hashbang? Stuff like that.



I *assume* my cgi setup is ok as I am running "Mail Graph" currently which is cgi driven and it works great. The permissions on my cgi scripts are the same as the mail graph cgi script so I think the permissions are ok. Not sure what "hashbang" is?

Is my entry in the sudoers file ok?


----------



## bigearsbilly (May 21, 2011)

Here's a perl CGI script I use, makes a table from df and refreshes every 45 seconds. 


```
#!/usr/bin/perl
use CGI qw(:standard);
print STDOUT header;
print start_html(-head=>meta({-http_equiv => 'Refresh',
                                    -content    => '45'}));
print hr;


$| = 1;
@fields = map { td [split " ", $_, 6] } (qx/df -ch/);
print table({border => 1}, Tr(\@fields));
```


----------



## bigearsbilly (May 21, 2011)

Is your script called .cgi? What directory is it in? Post the log of access denied, /var/log/http-error.log.

I would say, uncomment this in your httpd.conf:

```
Include etc/apache22/extra/httpd-userdir.conf
```
and then you can use ~/public_html directories

Also uncomment this:

```
Include etc/apache22/extra/httpd-manual.conf
```
and you will have the apache manual http://localhost/manual

And look at the UserDir directive.

Edit:    
You will need this to for the userdir cgis:

```
AddHandler cgi-script .cgi
```

make changes and then *sudo apachectl restart* or (preferred?) *sudo /usr/local/etc/apache22 restart*.


----------



## DutchDaemon (May 22, 2011)

xy16644 said:
			
		

> Is my entry in the sudoers file ok?



I seem to be missing the actual elevation of privileges there.

It would normally look something like:


```
www     ALL=(root) NOPASSWD: /path/to/command
```


----------

