Merge branch 'master' of git+ssh://github.com/adefaria/clearscm
[clearscm.git] / clients / GD / FSMon / FsmonDB.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 //
4 // File:        FsmonDB.php
5 // Description: PHP Module to access the fsmon database
6 // Author:      Andrew@ClearSCm.com
7 // Created:     Mon Apr 28 15:20:06 MST 2008
8 // Modified:    
9 // Language:    PHP
10 //
11 // (c) Copyright 2008, ClearSCM Inc., all rights reserved.
12 //
13 ////////////////////////////////////////////////////////////////////////////////
14 include_once ("Fsutils.php");
15
16 // DEBUG Flag
17 $debug = 1;
18
19 // Read only access
20 $userid         = "fsmon";
21 $password       = "fsmon";
22 $dbserver       = "seast1";
23 $dbname         = "fsmon";
24
25 // N/A
26 $na = "<font color=#999999>N/A</font>";
27
28 function dbError ($msg, $statement) {
29   $errno  = mysql_errno ();
30   $errmsg = mysql_error ();
31
32   print <<<END
33 <h2><font color="red">ERROR:</font> $msg</h2>
34 <b>Error #$errno:</b><br>
35 <blockquote>$errmsg</blockquote>
36 <b>SQL Statement:</b><br>
37 <blockquote>$statement</blockquote>
38 END;
39
40   exit ($errno);
41 } // dbError
42
43 function openDB () {
44   global $dbserver, $userid, $password, $dbname;
45
46   $db = mysql_connect ($dbserver, $userid, $password)
47     or dbError (__FUNCTION__ . ": Unable to connect to database server $dbserver", "Connect");
48
49   mysql_select_db ($dbname)
50     or dbError (__FUNCTION__ . ": Unable to select the $dbname database", "$dbname");
51 } // openDB
52
53 function getFSInfo ($system = "", $mount = "", $period = "daily") {
54   $sysCondition         = (isset ($system)) ? "where sysname = \"$system\"" : "";
55   $mountCondition       = (isset ($mount))  ? "  and mount   = \"$mount\""  : "";
56
57   if (!($period == "hourly"     or 
58         $period == "daily"      or
59         $period == "weekly"     or
60         $period == "monthly")) {
61     error ("Invalid period - $period - specified", 1);
62   } // if
63
64   $statement = <<<END
65 select
66   sysname,
67   mount,
68   timestamp,
69   size,
70   used,
71   free,
72   reserve
73 from
74   fs
75   $sysCondition
76   $mountCondition
77 order by
78   timestamp
79 END;
80
81   $result = mysql_query ($statement)
82     or DBError ("Unable to execute query: ", $statement);
83
84   $data = array ();
85
86   $lastPeriod   = "";
87   $maxUsed      = 0;
88
89   while ($row = mysql_fetch_array ($result)) {
90     $line["sysname"]    = $row["sysname"];
91     $line["mount"]      = $row["mount"];
92     $line["timestamp"]  = $row["timestamp"];
93
94     // Snapshot's finest granularity is 1 hour intervals. If hourly
95     // therefore just capture the data an move on.
96     if ($period == "hourly") {
97       $line["size"]     = $row["size"];
98       $line["used"]     = $row["used"];
99       $line["free"]     = $row["free"];
100       $line["reserve"]  = $row["reserve"];
101     } elseif ($period == "daily") {
102       $thisPeriod = substr ($row["timestamp"], 0, 10);
103
104       if ($lastPeriod == "") {
105         $lastPeriod = $thisPeriod;
106       } elseif ($lastPeriod == $thisPeriod) {
107         continue;
108       } // if
109       
110       $lastPeriod = $thisPeriod;
111
112       if ($row["used"] > $maxUsed) {
113         $maxUsed                = $row["used"];
114         $line["size"]           = $row["size"];
115         $line["used"]           = $row["used"];
116         $line["free"]           = $row["free"];
117         $line["reserve"]        = $row["reserve"];
118       } else {
119         continue;
120       } // if
121     } elseif ($period == "weekly") {
122       error ("Weekly not handled yet", 1);
123     } elseif ($period == "monthly") {
124       $thisPeriod = substr ($row["timestamp"], 0, 7);
125       if ($lastPeriod == "") {
126         $lastPeriod = $thisPeriod;
127       } elseif ($lastPeriod == $thisPeriod) {
128         continue;
129       } // if
130
131       if ($row["used"] > $maxUsed) {
132         $maxUsed                = $row["used"];
133         $line["size"]           = $row["size"];
134         $line["used"]           = $row["used"];
135         $line["free"]           = $row["free"];
136         $line["reserve"]        = $row["reserve"];
137       } else {
138         continue;
139       } // if
140     } // if
141
142     array_push ($data, $line);
143   } // while
144
145   return $data;
146 } // getFSInfo
147
148 function getSystem ($system = "") {
149   $statement = "select * from system";
150
151   if (isset ($system) and $system != "") {
152     $statement .= " where name = \"$system\"";
153   } // if
154
155   $result = mysql_query ($statement)
156     or DBError ("Unable to execute query: ", $statement);
157
158   $data = array ();
159
160   while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
161     array_push ($data, $row);
162   } // while
163
164   return $data;
165 } // getSystem
166
167 function getMounts ($system) {
168   $statement = "select mount from filesystems where sysname = \"$system\" order by mount";
169
170   $result = mysql_query ($statement)
171     or DBError ("Unable to execute query: ", $statement);
172
173   $data = array ();
174
175   while ($row = mysql_fetch_array ($result)) {
176     array_push ($data, $row["mount"]);
177   } // while
178
179   return $data;
180 } // getMounts
181 ?>