1 | #!/usr/bin/php -q |
---|
2 | |
---|
3 | # |
---|
4 | # dump_bfd : dumps usage totals per printer groups |
---|
5 | # |
---|
6 | # Contributed to this project by Jamuel P. Starkey on July 21st 2005 |
---|
7 | # |
---|
8 | # (c) 2005 Jamuel P. Starkey <jamuel@my740il.com> |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
22 | # |
---|
23 | # $Id$ |
---|
24 | # |
---|
25 | # |
---|
26 | |
---|
27 | <?php |
---|
28 | |
---|
29 | global $debug, $groups_all, $group_search, $printer_search, $descriptorspec, |
---|
30 | $dumpykota_options, $out_format; |
---|
31 | |
---|
32 | $descriptorspec = array( |
---|
33 | 0 => array("pipe", "r"), // stdin is a pipe that the child will read from |
---|
34 | 1 => array("pipe", "w"), // stdout is a pipe that the child will write to |
---|
35 | 2 => array("file", "/root/error-output.txt", "a") // stderr is a file to write to |
---|
36 | ); |
---|
37 | echo "\n"; |
---|
38 | $groups_all = "NULL"; |
---|
39 | $dumpykota_options = ''; |
---|
40 | $start = ''; |
---|
41 | $end = ''; |
---|
42 | $out_format = 'csv'; |
---|
43 | |
---|
44 | foreach ($argv as $args) |
---|
45 | { |
---|
46 | if ($args == '--debug=false') |
---|
47 | $debug = FALSE; |
---|
48 | if ($args == '--debug=true') |
---|
49 | $debug = TRUE; |
---|
50 | if (preg_match ('/\-\-printer(\=*)([a-z0-9\-\_]*)/i', $args, $printers)) |
---|
51 | { |
---|
52 | $printer_search = ($printers[2]); |
---|
53 | if ($printer_search == '') |
---|
54 | $printer_search = "NULL"; |
---|
55 | } |
---|
56 | if (preg_match ('/\-\-group(\=+)([a-z0-9\-\_]*)/i', $args, $groups)) |
---|
57 | { |
---|
58 | $group_search = ($groups[2]); |
---|
59 | if ($group_search == '') |
---|
60 | $group_search = "NULL"; |
---|
61 | } |
---|
62 | if ($args == '--groups') |
---|
63 | { |
---|
64 | $groups_all = TRUE; |
---|
65 | } |
---|
66 | if (preg_match ('/start=[a-z0-9\-]+/i', $args, $start)) |
---|
67 | { |
---|
68 | $dumpykota_options .= $start[0] . ' '; |
---|
69 | } |
---|
70 | if (preg_match ('/end=[a-z0-9\-]+/i', $args, $end)) |
---|
71 | { |
---|
72 | $dumpykota_options .= $end[0] . ' '; |
---|
73 | } |
---|
74 | if ($args == '--format=ssv') |
---|
75 | { |
---|
76 | $out_format = 'ssv'; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | if (!isset($debug)) |
---|
81 | $debug = FALSE; |
---|
82 | |
---|
83 | if ($debug) |
---|
84 | echo "Debugging ON\n\n"; |
---|
85 | |
---|
86 | function get_users () |
---|
87 | { |
---|
88 | global $debug, $printer_search, $descriptorspec; |
---|
89 | |
---|
90 | $users = array(); |
---|
91 | $usernames = array(); |
---|
92 | |
---|
93 | $cwd = '/root'; |
---|
94 | $env = array('some_option' => 'aeiou'); |
---|
95 | $in = "--data users"; |
---|
96 | $process = proc_open('dumpykota --data users --format ssv', $descriptorspec, $pipes); |
---|
97 | if (is_resource($process)) |
---|
98 | { |
---|
99 | fclose($pipes[0]); |
---|
100 | |
---|
101 | while (!feof($pipes[1])) |
---|
102 | { |
---|
103 | $out = fgets($pipes[1], 4096); |
---|
104 | $users[] = split (";", $out); |
---|
105 | } |
---|
106 | fclose($pipes[1]); |
---|
107 | |
---|
108 | $return_value = proc_close($process); |
---|
109 | array_shift ($users); |
---|
110 | array_pop ($users); |
---|
111 | if ($debug) |
---|
112 | echo "Got for Users:\n"; |
---|
113 | foreach ($users as $user) |
---|
114 | { |
---|
115 | list ($id, $username, $email, $balance, $lifetimepaid, $limitby, $overcharge) = $user; |
---|
116 | $username = trim ($username, '"'); |
---|
117 | $usernames[] = $username; |
---|
118 | if ($debug) |
---|
119 | echo "\t$username\n"; |
---|
120 | } |
---|
121 | } |
---|
122 | return ($usernames); |
---|
123 | } |
---|
124 | |
---|
125 | function get_printer_groups () |
---|
126 | { |
---|
127 | global $debug, $descriptorspec, $printer_groups; |
---|
128 | if ($debug) |
---|
129 | echo "\n"; |
---|
130 | $process = proc_open('dumpykota --data pmembers --format ssv', $descriptorspec, $pipes); |
---|
131 | if (is_resource($process)) |
---|
132 | { |
---|
133 | |
---|
134 | fclose($pipes[0]); |
---|
135 | |
---|
136 | $pgroups = array(); |
---|
137 | $printers = array(); |
---|
138 | $printer_groups = array(); |
---|
139 | $group_names = array(); |
---|
140 | |
---|
141 | while (!feof($pipes[1])) |
---|
142 | { |
---|
143 | $out = fgets($pipes[1], 4096); |
---|
144 | $pgroups[] = split (";", $out); |
---|
145 | if ($debug) |
---|
146 | echo "$out\n"; |
---|
147 | } |
---|
148 | fclose($pipes[1]); |
---|
149 | |
---|
150 | $return_value = proc_close($process); |
---|
151 | array_shift ($pgroups); |
---|
152 | array_pop ($pgroups); |
---|
153 | foreach ($pgroups as $pgroup) |
---|
154 | { |
---|
155 | //list ($id, $printername ,$description, $priceperpage, $priceperjob) = $user; |
---|
156 | list ($pgroupname, $printername, $groupid, $printerid) = $pgroup; |
---|
157 | |
---|
158 | $pgroupname = trim ($pgroupname, '"'); |
---|
159 | $printername = trim ($printername, '"'); |
---|
160 | |
---|
161 | if (!in_array ($pgroupname, $group_names)) |
---|
162 | { |
---|
163 | $printer_groups[$pgroupname][] = $printername; |
---|
164 | $group_names[] = $pgroupname; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | $printer_groups[$pgroupname][] = $printername; |
---|
169 | } |
---|
170 | if (!in_array ($printername, $printers)) |
---|
171 | { |
---|
172 | $printers[] = $printername; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | if ($debug) |
---|
179 | { |
---|
180 | echo "\n"; |
---|
181 | |
---|
182 | foreach ($printer_groups as $group_name => $group) |
---|
183 | { |
---|
184 | echo "$group_name:"; |
---|
185 | foreach ($group as $printer) |
---|
186 | { |
---|
187 | echo "\t" . $printer . "\n"; |
---|
188 | } |
---|
189 | echo "\n"; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | $my_groups = array(); |
---|
194 | //$printer_search = "None"; |
---|
195 | if (isset ($printer_search)) |
---|
196 | { |
---|
197 | if ($printer_search == "NULL") |
---|
198 | { |
---|
199 | echo "\nSyntax error check your --printer\n"; |
---|
200 | } |
---|
201 | elseif ( !in_array ($printer_search, $printers)) |
---|
202 | { |
---|
203 | echo "\n$printer_search not found. Check your printer name. (You didn't specify a printer group here did you?)\n"; |
---|
204 | } |
---|
205 | else |
---|
206 | { |
---|
207 | echo "\n$printer_search belongs to: "; |
---|
208 | foreach ($printer_groups as $group_name => $group) |
---|
209 | { |
---|
210 | foreach ($group as $printer) |
---|
211 | { |
---|
212 | if ($printer == $printer_search) |
---|
213 | { |
---|
214 | $my_groups[] = $group_name; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | if (count ($my_groups) == 0) |
---|
219 | { |
---|
220 | echo "NONE, 0, Zilch, Nada!\n"; |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | foreach ($my_groups as $group) |
---|
225 | { |
---|
226 | echo "$group "; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|
232 | return ($printer_groups); |
---|
233 | } |
---|
234 | function dump_by_group ($printer_groups, $group_search) |
---|
235 | { |
---|
236 | global $debug, $descriptorspec; |
---|
237 | global $dumpykota_options, $out_format; |
---|
238 | if ($out_format == 'ssv' ) |
---|
239 | { |
---|
240 | $delim = ';'; |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | $delim = ','; |
---|
245 | } |
---|
246 | if ($debug) |
---|
247 | echo "\n"; |
---|
248 | $history = array(); |
---|
249 | $entry = array(); |
---|
250 | |
---|
251 | $process = proc_open('dumpykota --data history --format ssv ' . $dumpykota_options, $descriptorspec, $pipes); |
---|
252 | if (is_resource($process)) |
---|
253 | { |
---|
254 | fclose($pipes[0]); |
---|
255 | while (!feof($pipes[1])) |
---|
256 | { |
---|
257 | $out = fgets($pipes[1], 4096); |
---|
258 | // if ($debug) |
---|
259 | // echo "$out\n"; |
---|
260 | $history[] = split (";", $out); |
---|
261 | } |
---|
262 | fclose($pipes[1]); |
---|
263 | |
---|
264 | $return_value = proc_close($process); |
---|
265 | } |
---|
266 | |
---|
267 | array_shift ($history); |
---|
268 | array_pop ($history); |
---|
269 | |
---|
270 | foreach ($history as $key => $value) |
---|
271 | { |
---|
272 | list ($username, $printername, $id, $jobid, $userid, $printerid, $pagecounter, $jobsizebytes, $jobsize, $jobprice, $action, $filename, $title, $copies,$options, $hostname, $md5sum, $pages,$billingcode, $jobdate) = $value; |
---|
273 | |
---|
274 | $entry[]= array( "username" => trim($username, '"'), |
---|
275 | "printername" => trim($printername, '"'), |
---|
276 | "id" => trim($id, '"'), |
---|
277 | "jobid" => trim($jobid, '"'), |
---|
278 | "userid" => trim($userid, '"'), |
---|
279 | "printerid" => trim($printerid, '"'), |
---|
280 | "pagecounter" => trim($pagecounter, '"'), |
---|
281 | "jobsizebytes" => trim($jobsizebytes, '"'), |
---|
282 | "jobsize" => trim($jobsize, '"'), |
---|
283 | "jobprice" => trim($jobprice, '"'), |
---|
284 | "action" => trim($action, '"'), |
---|
285 | "filename" => trim($filename, '"'), |
---|
286 | "title" => trim($title, '"'), |
---|
287 | "copies" => trim($copies, '"'), |
---|
288 | "options" => trim($options, '"'), |
---|
289 | "hostname" => trim($hostname, '"'), |
---|
290 | "md5sum" => trim($md5sum, '"'), |
---|
291 | "pages" => trim($pages, '"'), |
---|
292 | "billingcode" => trim($billingcode, '"'), |
---|
293 | "jobdate" => trim($jobdate, '"' . "\n") |
---|
294 | ); |
---|
295 | } |
---|
296 | |
---|
297 | if (isset ($group_search)) |
---|
298 | { |
---|
299 | if ($group_search == "NULL" ) |
---|
300 | { |
---|
301 | echo "\nSyntax error check your --group or --groups (you can't do both!)\n"; |
---|
302 | } |
---|
303 | elseif ( !array_key_exists ($group_search, $printer_groups)) |
---|
304 | { |
---|
305 | echo "\n$group_search not found. Check your group name. (You didn't specify a printer name here did you?)\n"; |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | $user_pages = array(); |
---|
310 | $usernames = get_users(); |
---|
311 | |
---|
312 | foreach ($usernames as $user) |
---|
313 | { |
---|
314 | $user_pages[$user] = 0; |
---|
315 | } |
---|
316 | foreach ($printer_groups[$group_search] as $printer) |
---|
317 | { |
---|
318 | foreach ($usernames as $user) |
---|
319 | { |
---|
320 | foreach ($entry as $value) |
---|
321 | { |
---|
322 | if ($value['printername'] == $printer && $value['username'] == $user) |
---|
323 | { |
---|
324 | $user_pages[$user ]= $user_pages[$user] + $value['jobsize']; |
---|
325 | } |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | foreach ($user_pages as $key => $value) |
---|
331 | echo '"' . $group_search . '"' . $delim . '"' . $key . '"' . $delim . '"' . $value . '"' . "\n"; |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | $printer_groups = get_printer_groups(); |
---|
337 | if ($group_search == "NULL" and !$groups_all) |
---|
338 | { |
---|
339 | echo "\nSyntax error check your --group or --groups (you can't do both!)\n"; |
---|
340 | } |
---|
341 | elseif ($group_search && $groups_all == "NULL") |
---|
342 | { |
---|
343 | dump_by_group ($printer_groups, $group_search); |
---|
344 | } |
---|
345 | elseif ($groups_all === TRUE) |
---|
346 | { |
---|
347 | foreach ($printer_groups as $group_name => $pgroup) |
---|
348 | { |
---|
349 | dump_by_group ($printer_groups, $group_name); |
---|
350 | } |
---|
351 | } |
---|
352 | elseif ($groups_all === "NULL") |
---|
353 | { |
---|
354 | echo "dump_bfd v0.2 Pykota Printer Group Dumper (c) 2005 GCI Systems\n"; |
---|
355 | echo "===============================================================\n"; |
---|
356 | echo "\nSYNOPSIS: dump_bfd [--groups || --group=<pykota_pgroup>] [start=<pykota_date_value> end=<pykota_date_value>]"; |
---|
357 | echo "\t [--format=<csv || ssv>][--debug=<true> || <false>]\n"; |
---|
358 | |
---|
359 | echo "\nDESCRIPTION:\n"; |
---|
360 | echo "\tDump pykota pgroup usage for all users by pgroup. Currently this utility dumps in CSV format only.\n" ; |
---|
361 | echo "\tThe column headings are PGroup Username, Pages Printed\n\n"; |
---|
362 | |
---|
363 | echo "\nOPTIONS:\n"; |
---|
364 | echo "\t--groups dumps all users usage by Pykota printer group.\n"; |
---|
365 | echo "\t--group=<pykota_pgroup> dumps all users usage for a single Pykota printer group.\n\n"; |
---|
366 | echo "\tstart=<pykota_date_value> sets the start date for the date filter using the same syntax as dumpykota.\n"; |
---|
367 | echo "\tend=<pykota_date_value> sets the end date for the date filter using the same syntax as dumpykota.\n"; |
---|
368 | echo "\t--format=<csv||ssv> sets the output of the command to comma or semicolon delimited format.\n"; |
---|
369 | echo "\nEX: \tdump-bfd --groups start=today-30 end=today --format=ssv\n"; |
---|
370 | echo "\tRetrieves results for all groups over the past 30 days and displays them in semicolon delimited format.\n\n"; |
---|
371 | |
---|
372 | echo "This program is free software; you can redistribute it and/or modify\n"; |
---|
373 | echo "it under the terms of the GNU General Public License as published by\n"; |
---|
374 | echo "the Free Software Foundation; either version 2 of the License, or\n"; |
---|
375 | echo "(at your option) any later version.\n"; |
---|
376 | echo "\n"; |
---|
377 | echo "This program is distributed in the hope that it will be useful,\n"; |
---|
378 | echo "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"; |
---|
379 | echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"; |
---|
380 | echo "GNU General Public License for more details.\n"; |
---|
381 | echo "\n"; |
---|
382 | echo "You can view the most recent GNU General Public License\n"; |
---|
383 | echo "by visiting http://www.gnu.org/licenses/gpl.txt or you can write to the \n"; |
---|
384 | echo "Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"; |
---|
385 | |
---|
386 | echo "\nSend support requests to jamuel@my740il.com\n\n"; |
---|
387 | } |
---|
388 | ?> |
---|