3 =head2 NAME $RCSfile: FsmonDB.pm,v $
5 Object oriented interface to filesystems
13 Andrew DeFaria <Andrew@ClearSCM.com>
21 Thu Dec 11 10:39:12 MST 2008
35 my $username = "fsmonadm";
36 my $password = "<password>";
38 my $fsmondb = new FsmonDB ($username, $password);
40 my ($errno, $errmsg) = $fsmondb->addSystem (
43 description => "Database server",
45 osversion => `uname -a`,
49 $status = $fsmondb->fsSnapshot (hostname);
53 Filesystem creates a filesystem object that encapsulates information
54 about the file system as a whole.
58 The following routines are exported:
76 ############################################################################
78 # insert: Construct SQL insert statement based on passed in table
79 # name and hash values.
83 # record Name value hash
86 # status ($errno, $errmsg)
88 ############################################################################
90 my ($self, $table, %values) = @_;
96 foreach (keys %values) {
108 $values .= "\"" . quotemeta ($values{$_}) . "\"";
112 my $statement = "insert into $table ($fields) values ($values)";
114 $self->{db}->do ($statement)
115 or return $self->_dberror ("Unable to add system", $statement);
120 ############################################################################
122 # _dberror: Output the DB error message and exit (Internal)
125 # msg User defined message to output
126 # statement SQL Statement attempted (optional)
131 ############################################################################
133 my ($self, $msg, $statement) = @_;
135 my $caller = (caller (1))[3];
137 my $returnMsg = "$caller: DBError: "
142 . $self->{db}->errstr;
144 $returnMsg .= "\nSQL Statement: $statement\n" if $statement;
146 return ($self->{db}->err, $returnMsg);
149 ############################################################################
151 # _exists: Return 1 if the value exists in the table otherwise 0
154 # table Name of table to search
155 # column Column name to search
156 # value Value to look for
157 # column2 Secondary column to search for
158 # value2 Secondary value to search for
161 # 1 if found, 0 if not
163 ############################################################################
164 sub _exists ($$$;$$) {
165 my ($self, $table, $column, $value, $column2, $value2) = @_;
167 my $statement = "select count(*) from $table where $column = \""
171 $statement .= " and $column2 = \""
172 . quotemeta ($value2)
177 unless ($sth = $self->{db}->prepare ($statement)) {
178 my ($errNo, $errMsg) = $self->_dberror ("Unable to prepare statement", $statement);
183 unless ($sth->execute) {
184 my ($errNo, $errMsg) = $self->_dberror ("Unable to execute statement", $statement);
189 my @row = $sth->fetchrow_array;
200 ############################################################################
202 # _count: Returns the number of entries in a table that qualify for
203 # the given condition.
206 # table Name of table to search
207 # id condition (Default: All entries in the table)
210 # Count of qualifying entries
212 ############################################################################
214 my ($self, $table, $condition) = @_;
216 $condition = $condition ? "where $condition" : "";
218 my $statement = "select count(*) from $table $condition";
222 unless ($sth = $self->{db}->prepare ($statement)) {
223 my ($errNo, $errMsg) = $self->_dberror ("Unable to prepare statement", $statement);
228 unless ($sth->execute) {
229 my ($errNo, $errMsg) = $self->_dberror ("Unable to execute statement", $statement);
234 my @row = $sth->fetchrow_array;
249 Construct a new FsmonDB object. The following OO style arguments are
254 =for html <blockquote>
262 =for html <blockquote>
270 =for html </blockquote>
275 my ($class, $username, $password) = @_;
277 $username = $username ? $username : "fsmon";
278 $password = $password ? $password : "fsmon";
280 my $dbname = $ENV{FSMON_DBNAME}
283 my $dbserver = $ENV{FSMON_DBSERVER}
284 ? $ENV{FSMON_DBSERVER}
286 my $dbdriver = "mysql";
288 my $db = DBI->connect ("DBI:$dbdriver:$dbname:$dbserver", $username,
289 $password, {PrintError => 0})
290 or croak "Unable to connect to $dbname database as $username";
294 username => $username,
295 password => $password,
301 =head3 addSystem (%system)
307 =for html <blockquote>
311 %system is a hash containing the following keys:
313 =item $name (required)
317 =item $owner (optional)
319 Person or persons responsible for this system
321 =item $description (optional)
323 Description of this system
325 =item $ostype (required)
327 An enumeration of "Linux", "Unix" or "Windows" (default Linux)
329 =item $osversion (optional)
331 String representing an OS version
335 =for html <blockquote>
343 =for html </blockquote>
348 my ($self, %record) = @_;
350 return $self->insert ("system", %record);
355 =head3 getSystem ($system)
361 =for html <blockquote>
365 =item $system (option)
367 Name of the system to return information about. If not specified then
368 getSystem returns all systems
372 =for html <blockquote>
376 =item If $system was specified, a hash of that system's
377 information. If $system is not specified then an array of hashes
378 containing information on all systems.
382 =for html </blockquote>
387 my ($self, $system) = @_;
389 my ($statement, $sth);
392 $statement = "select * from system where name = \"$system\"";
394 $statement = "select name from system";
397 unless ($sth = $self->{db}->prepare ($statement)) {
398 my ($errno, $errmsg) = $self->_dberror ("Unable to prepare statement", $statement);
399 error $errmsg, $errno;
402 unless ($sth->execute) {
403 my ($errno, $errmsg) = $self->_dberror ("Unable to execute statement", $statement);
404 error $errmsg, $errno;
408 return %{my $row = $sth->fetchrow_hashref};
412 while (my @record = $sth->fetchrow_array) {
413 push @records, pop @record;
422 =head3 addFilesystem ($system, $mount)
424 Add monitoring of a filesytem identified by $mount from a $system
428 =for html <blockquote>
432 =item $system (required)
434 Name of the system that this filesystem is local to
436 =item mount (optional)
438 Mount point for this file system. If undef then add all local file systems.
442 =for html <blockquote>
446 =item ($errno, $errmsg)
450 =for html </blockquote>
454 sub addFilesystem ($;$) {
455 my ($self, $system, $mount) = @_;
457 my $fs = new Filesystem;
459 foreach ($fs->mounts ()) {
460 my %fsinfo = $fs->getFSInfo ($_);
462 "sysname" => $system,
466 my ($errno, $errmsg);
470 ($errno, $errmsg) = $self->insert ("filesystems", %filesystem);
472 return ($errno, $errmsg) if $errno != 0;
475 ($errno, $errmsg) = $self->insert ("filesystems", %filesystem);
477 return ($errno, $errmsg) if $errno != 0;
486 =head3 addSnapshot (%snapshot)
488 Add a snapshot record of a filesystem
492 =for html <blockquote>
496 %snapshot is a hash containing the following keys:
498 =item sysname (required)
500 Name of the system that this filesystem is local to
502 =item mount (required)
504 Mount point for this file system
506 =item timestamp (required)
508 Timestamp representing the time that the snapshot of the filesystem
511 =item size (optional)
513 Total size of the filesystem in bytes
515 =item used (optional)
517 Number of bytes of used space
519 =item free (optional)
521 Number of bytes free or available for use
523 =item reserve (optional)
525 Number of bytes held in reserve
529 =for html <blockquote>
533 =item ($errno, $errmsg)
537 =for html </blockquote>
541 sub addSnapshot (%) {
542 my ($self, %snapshot) = @_;
544 return $self->insert ("fs", %snapshot);
549 =head3 snapshot ($system)
551 Take a snapshot of all configured file systems for a given system
555 =for html <blockquote>
561 Name of the system to snapshot
565 =for html <blockquote>
569 =item ($errno, $errmsg)
573 =for html </blockquote>
578 my ($self, $system) = @_;
580 my %system = $self->getSystem ($system);
581 my $fs = new Filesystem (
586 qr "$system{prompt}",
591 foreach ($fs->mounts ()) {
592 my %fsinfo = $fs->getFSInfo ($_);
596 $fs{sysname} = $system;
598 $fs{timestamp} = Today2SQLDatetime;
599 $fs{size} = $fsinfo{size};
600 $fs{used} = $fsinfo{used};
601 $fs{free} = $fsinfo{free};
602 $fs{reserve} = $fsinfo{reserve};
604 my ($errno, $errmsg) = $self->addSnapshot (%fs);
606 return ($errno, $errmsg) if $errno != 0;
617 FsmonDB - Access routines to the fsmon SQL database
625 This module provides for access routines to the fsmon SQL database.
629 =head2 new ($username, $password)
631 Opens the fsmon SQL database for the specified $username and
632 $password and returns a FsmonDB object
640 Username to connect to the database. At this time "fsmonadm" is the R/W
641 user and "fsmon" (password "reader") has R/O access (Default:
646 Password to use. (Default: "fsmon").
648 =item Returns FsmonDB object
652 =head2 addSystem (%testrun)
654 Adds the system record. Pass in a hash of field name/value pairs.
658 =item ($errno, $errmsg)
664 =head2 CONFIGURATION AND ENVIRONMENT
672 =head2 INCOMPATABILITIES
676 =head2 BUGS AND LIMITATIONS
678 There are no known bugs in this module.
680 Please report problems to Andrew DeFaria (Andrew@ClearSCM.com).
682 =head2 LICENSE AND COPYRIGHT
684 This Perl Module is freely available; you can redistribute it and/or
685 modify it under the terms of the GNU General Public License as
686 published by the Free Software Foundation; either version 2 of the
687 License, or (at your option) any later version.
689 This Perl Module is distributed in the hope that it will be useful,
690 but WITHOUT ANY WARRANTY; without even the implied warranty of
691 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
692 General Public License (L<http://www.gnu.org/copyleft/gpl.html>) for more
695 You should have received a copy of the GNU General Public License
696 along with this Perl Module; if not, write to the Free Software Foundation,
697 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.