-
Notifications
You must be signed in to change notification settings - Fork 10
/
run_schedule.php
222 lines (179 loc) · 5.92 KB
/
run_schedule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
chdir(__DIR__ . '/../../');
include('./include/cli_check.php');
include_once($config['base_path'] . '/lib/poller.php');
include_once($config['base_path'] . '/lib/time.php');
include_once($config['base_path'] . '/plugins/flowview/database.php');
include_once($config['base_path'] . '/plugins/flowview/setup.php');
include_once($config['base_path'] . '/plugins/flowview/functions.php');
flowview_connect();
ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1');
$debug = false;
$maint = false;
$force = false;
$scheduled = false;
$report_id = false;
$shortopts = 'VvHh';
$longopts = array(
'scheduled::',
'report-id::',
'debug',
'force',
'version',
'help',
);
$options = getopt($shortopts, $longopts);
foreach($options as $arg => $value) {
switch($arg) {
case 'force':
$force = true;
break;
case 'debug':
$debug = true;
break;
case 'scheduled':
$scheduled = true;
break;
case 'report-id':
$report_id = $value;
break;
case 'version':
display_version();
exit(0);
break;
case 'help':
display_help();
exit(0);
break;
default:
print 'ERROR: Invalid option ' . $arg . PHP_EOL;
exit(1);
break;
}
}
if ($scheduled == true) {
/* silently end if the registered process is still running, or process table missing */
if (!$force) {
if (!register_process_start('flowsched', 'master', 0, 1200)) {
exit(0);
}
}
$start = microtime(true);
$run = 0;
$concurrent_processes = read_config_option('reports_concurrent');
if (empty($concurrent_processes)) {
set_config_option('reports_concurrent', 1);
$concurrent_processes = 1;
}
$reports = db_fetch_assoc_prepared('SELECT *
FROM reports_queued
WHERE status = ?
AND source = ?',
array('pending', 'flowview'));
while(true) {
$running = db_fetch_cell_prepared('SELECT COUNT(*)
FROM processes
WHERE taskname = ?
AND tasktype LIKE ?',
array('flowsched', 'child%'));
if ($running < $concurrent_processes) {
$report = db_fetch_row_prepared('SELECT *
FROM reports_queued
WHERE source = ?
AND status = ?
LIMIT 1',
array('flowview', 'pending'));
if (cacti_sizeof($report)) {
reports_run($report['id']);
$run++;
} else {
break;
}
sleep(1);
}
}
$end = microtime(true);
cacti_log(sprintf('FLOWVIEW REPORT STATS: Time:%0.2f Reports:%s', $end - $start, $run), false, 'SYSTEM');
if (!$force) {
unregister_process('flowsched', 'master', 0);
}
} else {
if ($report_id === false) {
cacti_log('FATAL: Flowview Schedule Report ID not provided', true, 'FLOWVIEW');
exit(1);
}
$report = db_fetch_row_prepared('SELECT rq.*
FROM reports_queued AS rq
WHERE rq.source = ?
AND rq.id = ?',
array('flowview', $report_id));
if (cacti_sizeof($report)) {
$id = $report['source_id'];
/* silently end if the registered process is still running, or process table missing */
if (!$force) {
if (!register_process_start('flowsched', "child_$id", 0, 1200)) {
exit(0);
}
}
$t = time();
$r = intval($t / 60) * 60;
$start = microtime(true);
$schedule = flowview_db_fetch_row_prepared('SELECT *
FROM plugin_flowview_schedules
WHERE id = ?',
array($id));
if (cacti_sizeof($schedule)) {
flowview_db_execute_prepared('UPDATE plugin_flowview_schedules
SET lastsent = ?, start = ?
WHERE id = ?',
array($r, date('Y-m-d H:i:s'), $id));
plugin_flowview_run_schedule($id, $report_id);
$end = microtime(true);
$cacti_stats = sprintf('Time:%01.4f Schedule:%s', round($end-$start,2), $id);
cacti_log('FLOWVIEW SCHEDULE STATS: ' . $cacti_stats , true, 'SYSTEM');
}
if (!$force) {
unregister_process('flowsched', "child_$id", 0);
}
} else {
cacti_log("WARNING: Unable to find Report ID $report_id", false, 'REPORTS');
}
}
function display_version() {
$version = get_cacti_cli_version();
print "Cacti FlowView Schedule Poller, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
function display_help() {
display_version();
print PHP_EOL . 'usage: schedule_run.php --schedule=ID [--debug]' . PHP_EOL . PHP_EOL;
print 'Runs the Selected Report and Sends to Receivers.' . PHP_EOL . PHP_EOL;
print 'Required:' . PHP_EOL;
print ' --schedule=ID The Schedule to Run.' . PHP_EOL . PHP_EOL;
print 'Options:' . PHP_EOL;
print ' --force Force running even if another is running.' . PHP_EOL . PHP_EOL;
print ' --debug Provide some debug output during collection.' . PHP_EOL . PHP_EOL;
}