root/trunk/flowmon-web/db_handler.inc.php

Revision 1, 4.5 kB (checked in by ixs, 16 years ago)

initial checkin of RH revision

Line 
1 <?php
2 #
3 # Copyright (C) 2008 Red Hat, Inc.
4 # Author: Andreas Thienemann <athienem@redhat.com>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Library General Public License as published by
8 # the Free Software Foundation; version 2 only
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Library General Public License for more details.
14 #
15 # You should have received a copy of the GNU Library General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Copyright 2004, 2005 Red Hat, Inc.
19 #
20 # AUTHOR: Andreas Thienemann <athienem@redhat.com>
21 #
22
23
24 // initiate the database connection
25 function init_db() {
26     global $dbh, $db_host, $db_name, $db_user, $db_pass;
27     try {
28         if ($database = 'mysql') {
29             $dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
30         } else if ($database = 'postgresql') {
31             $dbh = new PDO('pgsql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
32         }
33         $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
34     } catch (PDOException $e) {
35         print "Error!: " . $e->getMessage() . "<br/>";
36         die();
37     }
38 }
39
40 // return the number of reporting probes
41 function get_num_probes() {
42     global $dbh;
43     try {
44         $q = $dbh->query("SELECT COUNT(FlowSrc) FROM FlowSources;");
45         $r = $q->fetch();
46     } catch (PDOException $e) {
47         print "Error!: " . $e->getMessage() . "<br/>";
48         die();
49     }
50
51     return $r[0];
52 }
53
54 // return the number of archived flows
55 function get_num_flows() {
56     global $dbh;
57     try {
58         $q = $dbh->query("SELECT COUNT(FlowSrc) FROM Flows;");
59         $r = $q->fetch();
60     } catch (PDOException $e) {
61         print "Error!: " . $e->getMessage() . "<br/>";
62         die();
63     }
64
65     return $r[0];
66 }
67
68 // return the number of systems in the databse
69 function get_num_systems() {
70     global $dbh;
71     try {
72         $q = $dbh->query("SELECT COUNT(DISTINCT IPSrc) FROM Flows;");
73         $r = $q->fetch();
74     } catch (PDOException $e) {
75         print "Error!: " . $e->getMessage() . "<br/>";
76         die();
77     }
78
79     return $r[0];
80 }
81
82 // return the oldest flow in the db as a unix timestamp
83 function get_oldest_flow_ts() {
84     global $dbh;
85     try {
86         $q = $dbh->query("SELECT MIN(TimeStart) FROM Flows;");
87         $r = $q->fetch();
88     } catch (PDOException $e) {
89         print "Error!: " . $e->getMessage() . "<br/>";
90         die();
91     }
92
93     return $r[0];
94 }
95
96 // return the oldest flow in the db as a unix timestamp
97 function get_newest_flow_ts() {
98     global $dbh;
99     try {
100         $q = $dbh->query("SELECT MAX(TimeStop) FROM Flows;");
101         $r = $q->fetch();
102     } catch (PDOException $e) {
103         print "Error!: " . $e->getMessage() . "<br/>";
104         die();
105     }
106
107     return $r[0];
108 }
109
110 // return the total traffic in the archive
111 function get_bytes() {
112     global $dbh;
113     try {
114         $q = $dbh->query("SELECT SUM(Bytes) FROM Flows;");
115         $r = $q->fetch();
116     } catch (PDOException $e) {
117         print "Error!: " . $e->getMessage() . "<br/>";
118         die();
119     }
120
121     return $r[0];
122 }
123
124 // return the total traffic in the archive
125 function get_pkts() {
126     global $dbh;
127     try {
128         $q = $dbh->query("SELECT SUM(Pakets) FROM Flows;");
129         $r = $q->fetch();
130     } catch (PDOException $e) {
131         print "Error!: " . $e->getMessage() . "<br/>";
132         die();
133     }
134
135     return $r[0];
136 }
137
138 // return the database size
139 function get_db_size() {
140     global $dbh;
141     try {
142         $size = 0;
143         foreach($dbh->query("SHOW TABLE STATUS") as $row) {
144             $size += $row['Data_length'] + $row['Index_length'];
145         }
146     } catch (PDOException $e) {
147         print "Error!: " . $e->getMessage() . "<br/>";
148         die();
149     }
150     return $size;
151 }
152
153 // return existing FlowProbes
154 function get_probes() {
155     global $dbh;
156     $probes = array();
157     try {
158         foreach($dbh->query("SELECT FlowSrc, Hostname FROM FlowSources") as $row) {
159             // No hostname found in DB, retrieve it from the DNS
160             $ip = long2ip($row['FlowSrc']);
161             if (empty($row['Hostname'])) {
162                 $row['Hostname'] = gethostbyaddr($ip);
163             }
164             $probes[$ip] = $row['Hostname'];
165         }
166     } catch (PDOException $e) {
167         print "Error!: " . $e->getMessage() . "<br/>";
168         die();
169     }
170     return $probes;
171 }
172
173 // check if a probe definition is valid
174 // expects an ip address
175 function isvalid_probe($ip) {
176     global $dbh;
177     $probes = array();
178     try {
179         $q = $dbh->prepare("SELECT FlowSrc FROM FlowSources WHERE FlowSrc = ?;");
180         $q->bindParam(1, sprintf("%u", ip2long($ip)));
181         $q->execute();
182         $r = $q->fetch();
183     } catch (PDOException $e) {
184         print "Error!: " . $e->getMessage() . "<br/>";
185         die();
186     }
187     if ($ip == long2ip($r[0])) {
188         return True;
189     }
190     return False;
191 }
192
193 //$dbh = '';
194 //init_db();
195
Note: See TracBrowser for help on using the browser.