fix ticketing system error on bootstrap of new install
[freeside.git] / torrus / bin / flushmonitors.in
1 #!@PERL@ -w
2 #  Copyright (C) 2010  Stanislav Sinyagin
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 # $Id: flushmonitors.in,v 1.1 2010-12-27 00:04:01 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 BEGIN { require '@torrus_config_pl@'; }
22
23 use Getopt::Long;
24 use strict;
25
26 use Torrus::SiteConfig;
27 use Torrus::ConfigTree;
28 use Torrus::Log;
29
30 exit(1) if not Torrus::SiteConfig::verify();
31
32 our @trees;
33 our $all_trees;
34
35 our $debug;
36 our $verbose;
37 our $help_needed;
38
39 my $ok = GetOptions ('tree=s'   => \@trees,
40                      'all'      => \$all_trees,
41                      'debug'    => \$debug,
42                      'verbose'  => \$verbose,
43                      'help'     => \$help_needed);
44
45 if( not $ok or not (scalar(@trees) or $all_trees) or
46     $help_needed or scalar(@ARGV) > 0 )
47 {
48     print STDERR "Usage: $0 --tree=NAME [options...]\n",
49     "The utility flushes all monitor alarms and dynamic tokenset members\n",
50     "Options:\n",
51     "  --tree=NAME     tree name(s) to flush\n",
52     "  --all           flush all trees\n",
53     "  --debug         set the log level to debug\n",
54     "  --verbose       set the log level to info\n",
55     "  --help          this help message\n";
56     exit 1;
57 }
58
59 if( $all_trees )
60 {
61     @trees = Torrus::SiteConfig::listTreeNames();
62 }
63
64 if( $debug )
65 {
66     Torrus::Log::setLevel('debug');
67 }
68 elsif( $verbose )
69 {
70     Torrus::Log::setLevel('verbose');
71 }
72
73
74 &Torrus::DB::setSafeSignalHandlers();
75
76 Verbose(sprintf('Torrus version %s', '@VERSION@'));
77
78 foreach my $tree ( @trees )
79 {
80     if( not Torrus::SiteConfig::treeExists( $tree ) )
81     {
82         Error("Tree named \"" . $tree . "\" does not exist");
83         exit(1);
84     }
85
86     &Torrus::DB::checkInterrupted();
87     
88     Verbose("Flushing alarms and tokensets for the tree: $tree");
89
90     my $config_tree = new Torrus::ConfigTree( -TreeName => $tree,
91                                               -Wait => 1 );
92     if( not defined( $config_tree ) )
93     {
94         next;
95     }
96
97     my $db = new Torrus::DB('monitor_alarms',
98                             -Subdir => $tree,
99                             -WriteAccess => 1);
100
101
102     my $cursor = $db->cursor(-Write => 1);
103     while( my ($key, $timers) = $db->next($cursor) )
104     {
105         Debug('Deleting alarm: ' . $key);        
106         $db->c_del( $cursor );            
107         
108     }
109     undef $cursor;   
110     undef $db;
111     
112     &Torrus::DB::checkInterrupted();
113
114     my @members;
115     foreach my $ts ( $config_tree->getTsets() )
116     {
117         Debug('Processing tokenset: ' . $ts);
118         
119         foreach my $member ( $config_tree->tsetMembers( $ts ) )
120         {
121             my $origin = $config_tree->tsetMembers( $ts, $member );
122             
123             if( not defined( $origin ) or $origin ne 'static' )
124             {
125                 my $path = $config_tree->path($member);
126                 $config_tree->tsetDelMember($ts, $member);
127                 Verbose('deleted ' . $path . ' from tokenset: ' . $ts);
128             }
129         }
130     }
131
132     undef $config_tree;
133     &Torrus::DB::cleanupEnvironment();
134 }
135
136 exit;
137
138
139 # Local Variables:
140 # mode: perl
141 # indent-tabs-mode: nil
142 # perl-indent-level: 4
143 # End: