support.inc.php
18.7 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<?php
/**
* $Id:$
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008 KnowledgeTree Inc.
* Portions copyright The Jam Warehouse Software (Pty) Limited
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*
*/
/**
* TODO: refactor into seperate comparison object
*
*/
class MD5SourceTree
{
private $rootDir;
private $logFilename;
private $logFile;
private $numDirectories;
private $numFiles;
private $comparisonFailure;
private $exclusions;
public function __construct($exclusions = array())
{
$this->numDirectories = 0;
$this->numFiles = 0;
$this->exclusions = $exclusions;
}
/**
* Helper function to traverse the directories. Called initially by scan()
*
* @param string $dir
*/
private function _scan($dir)
{
if (in_array($dir, $this->exclusions))
{
return;
}
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($filename = readdir($dh)) !== false)
{
if (substr($filename,0,1) == '.')
{
continue;
}
$path = $dir . '/' . $filename;
if (is_dir($path))
{
$this->numDirectories++;
$this->_scan($path);
}
else
{
$this->numFiles++;
if (is_readable($path))
{
$md5 = md5_file($path);
$path = substr($path, strlen($this->rootDir) + 1);
fwrite($this->logFile, "$md5:$path\n");
}
}
}
closedir($dh);
}
}
}
/**
* This does the scan of the directory.
*
* @param string $rootDir
* @param string $reportFile
*/
public function scan($rootDir, $reportFile)
{
$this->rootDir = $rootDir;
$this->logFilename = $reportFile;
$this->logFile = fopen($reportFile,'wt');
$this->_scan($rootDir);
fclose($this->logFile);
}
/**
* Used by the compare function, to load a md5 file
*
* @param string $path
* @return array
*/
private function _loadDirectory($path)
{
$dirs = array();
$numFiles = 0;
$numDirectories = 0;
$fp = fopen($path, 'rt');
while (!feof($fp))
{
$line = fgets($fp, 10240);
list($md5, $path) = explode(':',$line);
$dirname = dirname($path);
$filename = basename($path);
$numFiles++;
$dirs[$dirname][$filename] = $md5;
}
fclose($fp);
return array('numFiles'=>$numFiles, 'numDirectories'=>$numDirectories, 'dirs'=>$dirs);
}
/**
* Internal function used to compare two md5 directory structures.
*
* @param array $prev
* @param array $cur
* @param string $msg
*/
private function _compare($prev, $cur, $msg)
{
foreach($prev['dirs'] as $prevDir=>$prevDirFiles)
{
if (!array_key_exists($prevDir, $cur['dirs']))
{
print "$msg: $prevDir does not exist in target.\n";
}
else
{
foreach($prevDirFiles as $prevFilename=>$prevMD5)
{
if (!array_key_exists($prevFilename, $cur['dirs'][$prevDir]))
{
$prevFilename = substr($prevFilename,0,-1);
print "$msg: $prevFilename does not exist in $prevDir.\n";
}
else
{
if (in_array($prevDir . '/' . $prevFilename, $this->comparisonFailure))
{
continue;
}
$newMD5 = $cur['dirs'][$prevDir][$prevFilename];
if ($prevMD5 != $newMD5)
{
$this->comparisonFailure[] = $prevDir . '/' . $prevFilename;
$prevFilename = substr($prevFilename,0,-1);
print "$msg: $prevFilename does not match md5; $prevMD5 != $newMD5.\n";
}
}
}
}
}
}
/**
* Compare to md5 report files
*
* @param string $reportA
* @param string $reportB
*/
public function compare($reportA, $reportB)
{
if (is_null($reportB))
{
$reportB = $this->logFilename;
}
$this->comparisonFailure = array();
$prev = $this->_loadDirectory($reportA);
$cur = $this->_loadDirectory($reportB);
if ($prev['numDirectories'] != $cur['numDirectories'])
{
print "Folder count mismatch!\n";
}
if ($prev['numFiles'] != $cur['numFiles'])
{
print "File count mismatch!\n";
}
$this->_compare($prev, $cur,'>');
$this->_compare($cur,$prev,'<');
}
}
class SupportUtil
{
private $path;
private $innodb;
private $noninnodb;
/**
* Constructor for SupportUtil. Creates a folder with format support-YYYY-MM-DD_HH-mm-ss
*
*/
function __construct()
{
$config = KTConfig::getSingleton();
$tempdir = $config->get('urls/tmpDirectory');
$this->path = $tempdir . "/support-" . date('Y-m-d_H-i-s');
mkdir($this->path);
}
/**
* Main function to capture as much info that is reasonable.
*
*/
public function capture()
{
// get php info
$this->capture_phpinfo($this->path . '/phpinfo.htm');
// get db schema
$tables = $this->capture_db_schema($this->path);
// get zseq counters from taables
$this->capture_zseqs($tables, $this->path . '/zseqreport.htm');
// get md5 on table
$exclusions = array(
KT_DIR . '/var',
realpath(KT_DIR . '/../var')
);
$tree = new MD5SourceTree($exclusions);
$config = KTConfig::getSingleton();
$sourcePath = $config->get('KnowledgeTree/fileSystemRoot');
$tree->scan($sourcePath, $this->path . '/md5report.txt');
// get plugins
$this->capture_plugins($this->path . '/plugins.htm');
// get logs
$this->capture_logs($this->path);
// get sys info
$this->get_sysinfo($this->path);
// get storage engine list
$this->create_storage_engine($this->path);
// get disk space listing
$this->capture_df($this->path);
// get process listing
$this->capture_ps($this->path);
// get version files
$this->capture_version_files($this->path);
// get system settings
$this->capture_system_settings($this->path);
// create out index file
$this->create_index($this->path);
}
/**
* Main helper function to cleanup after creating zip file
*
* @param stirng $path
*/
private function _cleanup($path)
{
$dh = opendir($path);
while (($filename = readdir($dh)) !== false)
{
if (substr($filename,0,1) == '.') continue;
$fullname = $path . '/' . $filename;
if (is_dir($fullname))
{
$this->_cleanup($fullname);
}
else
{
unlink($fullname);
}
}
closedir($dh);
rmdir($path);
}
/**
* Main cleanup function
*
*/
public function cleanup()
{
$this->_cleanup($this->path);
}
/**
* Creates an archive file
*
* @return string
*/
public function archive()
{
$zip = KTUtil::findCommand('export/zip', 'zip');
chdir(dirname($this->path));
$subdir = basename($this->path);
$archivename = $this->path . '.zip';
$cmd = "\"$zip\" -r \"$archivename\" \"$subdir\"";
KTUtil::pexec($cmd);
return $archivename;
}
/**
* Tries to get list of running processes
*
* @param string $path
*/
private function capture_ps($path)
{
$ps = KTUtil::findCommand('externalBinary/ps', 'ps');
if (!file_exists($ps) || !is_executable($ps))
{
return;
}
$cmd = "'$ps' waux";
// TODO: refactor to use KTUtil::pexec
$ps = popen($cmd, 'r');
$content = fread($ps , 10240);
pclose($ps);
file_put_contents($path . '/ps.txt', $content);
}
/**
* Get list of KnowledgeTree version files
*
* @param string $path
*/
private function capture_version_files($path)
{
$path = $path . '/versions';
mkdir($path);
$ver_path = KT_DIR . '/docs';
$dh = opendir($ver_path);
while (($filename = readdir($dh)) !== false)
{
if (substr($filename, 0, 7) == 'VERSION')
{
copy($ver_path . '/' . $filename, $path . '/' . $filename);
}
}
closedir($dh);
}
/**
* Dump the system_settings table, except for dashboard-state entries.
*
* @param string $path
*/
private function capture_system_settings($path)
{
$sql = "SELECT id, name, value FROM system_settings";
$rs = DBUtil::getResultArray($sql);
$html = "<h1>System Settings</h1>";
$html .= '<br><table border=1 cellpadding=0 cellspacing=0>';
foreach($rs as $rec)
{
$id = $rec['id'];
$name = $rec['name'];
$value = $rec['value'];
if (substr($name, 0, 15) == 'dashboard-state') continue;
$html .= "<tr><td>$id<td>$name<td>$value\r\n";
}
$html .= '</table>';
file_put_contents($path . '/systemsettings.htm', $html);
}
/**
* Get disk usage
*
* @param string $path
*/
private function capture_df($path)
{
$df = KTUtil::findCommand('externalBinary/df', 'df');
if (!file_exists($df) || !is_executable($df))
{
return;
}
$df = popen($df, 'r');
$content = fread($df, 10240);
pclose($df);
file_put_contents($path . '/df.txt', $content);
}
/**
* Get php info
*
* @param string $filename
*/
private function capture_phpinfo($filename)
{
ob_start();
phpinfo();
$phpinfo = ob_get_clean();
file_put_contents($filename, $phpinfo);
}
/**
* Helper table to get schema
*
* @param string $folder
* @return string
*/
private function capture_table_schema($folder)
{
$tables = array();
$sql = 'show tables';
$results = DBUtil::getResultArray($sql);
foreach($results as $rec)
{
$rec = array_values($rec);
$tablename = $rec[0];
$sql = "show create table $tablename";
$sql = DBUtil::getOneResultKey($sql,'Create Table');
file_put_contents($folder . '/' . $tablename . '.sql.txt', $sql);
$sql = strtolower($sql);
if (strpos($sql, 'innodb') === false)
$this->noninnodb[] = $tablename;
else
$this->innodb[] = $tablename;
$tables[] = $tablename;
}
return $tables;
}
/**
* Get database schema
*
* @param string $folder
* @param string $suffix
* @return array
*/
private function capture_db_schema($folder, $suffix='')
{
$schema_folder = $folder . '/' . $suffix . 'schema';
mkdir($schema_folder);
return $this->capture_table_schema($schema_folder);
}
/**
* Get list of plugins
*
* @param string $filename
*/
private function capture_plugins($filename)
{
$sql = 'select namespace,path, disabled, unavailable,friendly_name from plugins';
$result = DBUtil::getResultArray($sql);
$plugins = "<h1>Plugin Status Report</h1>";
$plugins .= '<table border=1 cellpadding=0 cellspacing=0u >';
$plugins .= '<tr><th>Display Name<th>Availability<th>Namespace<th>Path';
foreach($result as $rec)
{
$fileexists = file_exists(KT_DIR . '/' . $rec['path'])?'':'<font color="red">';
$status = ($rec['disabled'] == 0)?'<font color="green">':'<font color="orange">';
$unavailable = ($rec['unavailable'] == 0)?'available':'<font color="orange">unavailable';
$plugins .= '<tr>';
$plugins .= '<td>' . $status . $rec['friendly_name'];
$plugins .= '<td>' . $unavailable;
$plugins .= '<td>' . $rec['namespace'];
$plugins .= '<td>' . $fileexists . $rec['path'] . "\r\n";
}
$plugins .= '</table>';
$plugins .= '<br>Plugin name is <font color=green>green</font> if enabled and <font color=orange>orange</font> if disabled .';
$plugins .= '<br>Availability indicates that KnowledgeTree has detected the plugin not to be available.';
$plugins .= '<br>Path is coloured <font color=red>red</font> if the plugin file cannot be resolved. If the path is not resolved, it should be flagged unavailable.';
file_put_contents($filename, $plugins);
}
/**
* Make a zseq report
*
* @param string $tables
* @param string $filename
*/
private function capture_zseqs($tables, $filename)
{
$zseqs = '<h1>Table Counter Report</h1>';
$zseqs .= '<table border=1 cellpadding=0 cellspacing=0>';
$zseqs .= '<tr><td>Table<td>Max ID<td>ZSEQ<td>Status';
foreach($tables as $ztablename)
{
if (substr($ztablename, 0, 5) != 'zseq_')
{
continue;
}
$tablename = substr($ztablename, 5);
$sql = "SELECT max(id) as maxid FROM $tablename";
$maxid = DBUtil::getOneResultKey($sql, 'maxid');
$sql = "SELECT id FROM $ztablename";
$zseqid = DBUtil::getOneResultKey($sql, 'id');
$note = (is_null($maxid) || $maxid <= $zseqid)?'OK':'FAIL';
if ($note == 'FAIL' && $maxid > $zseqid)
{
$note = 'COUNTER PROBLEM! maxid should be less than or equal to zseq';
}
if (PEAR::isError($maxid))
{
$maxid = '??';
$note = "STRANGE - DB ERROR ON $tablename";
}
if (PEAR::isError($zseqid))
{
$zseqid = '??';
$note = "STRANGE - DB ERROR ON $ztablename";
}
if (is_null($maxid))
{
$maxid='empty';
}
if (is_null($zseqid))
{
$zseqid='empty';
$note = "STRANGE - ZSEQ SHOULD NOT BE EMPTY ON $ztablename";
}
$zseqs .= "<tr><td>$tablename<td>$maxid<td>$zseqid<td>$note\r\n";
}
$zseqs .= "</table>";
file_put_contents($filename, $zseqs);
}
/**
* Get log files
*
* @param string $path
*/
private function capture_logs($path)
{
$path = $path . '/logs';
mkdir($path);
$this->capture_kt_log($path);
$this->capture_apache_log($path);
$this->capture_php_log($path);
$this->capture_mysql_log($path);
}
/**
* Get Php log file. KT makes a php_error_log when tweak setting is enabled.
*
* @param string $path
*/
private function capture_php_log($path)
{
$config = KTConfig::getSingleton();
$logdir = $config->get('urls/logDirectory');
$logfile = $logdir . '/php_error_log';
if (file_exists($logfile))
{
copy($logfile, $path . '/php-error_log.txt');
}
}
/**
* Get mysql log from stack. It is difficult to resolve otherwise.
*
* @param string $path
*/
private function capture_mysql_log($path)
{
$stack_path = realpath(KT_DIR . '/../mysql/data');
if ($stack_path === false || !is_dir($stack_path))
{
return;
}
$dh = opendir($stack_path);
while (($filename = readdir($dh)) !== false)
{
if (substr($filename, -4) == '.log' && strpos($filename, 'err') !== false)
{
copy($stack_path . '/' . $filename, $path . '/mysql-' . $filename);
}
}
closedir($dh);
}
/**
* Get Apache log file from stack. It is difficult to resolve otherwise.
*
* @param string $path
*/
private function capture_apache_log($path)
{
$stack_path = realpath(KT_DIR . '/../apache2/logs');
if ($stack_path === false || !is_dir($stack_path))
{
return;
}
$dh = opendir($stack_path);
while (($filename = readdir($dh)) !== false)
{
if (substr($filename, -4) == '.log' && strpos($filename, 'err') !== false)
{
copy($stack_path . '/' . $filename, $path . '/apache-' . $filename);
}
}
closedir($dh);
}
/**
* Get KT log file.
*
* @param string $path
*/
private function capture_kt_log($path)
{
$date = date('Y-m-d');
$config = KTConfig::getSingleton();
$logdir = $config->get('urls/logDirectory');
$dh = opendir($logdir);
while (($filename = readdir($dh)) !== false)
{
if (substr($filename,0,14) != 'log-' . $date)
{
continue;
}
copy($logdir . '/' . $filename, $path . '/kt-' . $filename);
}
closedir($dh);
}
/**
* Get some basic info on Linux if possible. Get cpuinfo, loadavg, meminfo
*
* @param string $path
*/
private function get_sysinfo($path)
{
if (!OS_UNIX && !is_dir('/proc'))
{
return;
}
$path .= '/sysinfo';
mkdir($path);
$this->get_sysinfo_file('cpuinfo', $path);
$this->get_sysinfo_file('loadavg', $path);
$this->get_sysinfo_file('meminfo', $path);
}
/**
* Helper to get linux sysinfo
*
* @param string $filename
* @param string $path
*/
private function get_sysinfo_file($filename, $path)
{
if (!is_readable('/proc/' . $filename))
{
return;
}
$content = file_get_contents('/proc/' . $filename);
file_put_contents($path . '/' . $filename . '.txt', $content);
}
/**
* Helper to create the index file for the support archive.
*
* @param string $title
* @param string $path
* @param boolean $relative
* @return string
*/
private function get_index_contents($title, $path, $relative = true)
{
if (!is_dir($path))
{
return '';
}
$contents = array();
$dh = opendir($path);
while (($filename = readdir($dh)) !== false)
{
if (substr($filename,0,1) == '.') continue;
$fullname = $path . '/' . $filename;
if (!file_exists($fullname) || is_dir($fullname))
{
continue;
}
$contents[] = $fullname;
}
closedir($dh);
sort($contents);
$html = $title;
if (empty($contents))
{
$html .= 'There is no content for this section.';
return $html;
}
$dir = '';
if ($relative) $dir = basename($path) . '/';
foreach($contents as $filename)
{
$corename = basename($filename);
$ext = pathinfo($corename, PATHINFO_EXTENSION);
$basename = substr($corename, 0, -strlen($ext)-1);
$html .= "<a href=\"$dir$corename\">$basename</a><br>";
}
return $html;
}
/**
* Create the support archvie index.htm
*
* @param string $path
*/
private function create_index($path)
{
$contents = $this->get_index_contents('<h1>Support Info</h1><br>', $path, false);
$contents .= $this->get_index_contents('<h2>System Info</h2>', $path . '/sysinfo');
$contents .= $this->get_index_contents('<h2>Logs</h2>', $path . '/logs');
$contents .= $this->get_index_contents('<h2>Schema</h2>', $path . '/schema');
file_put_contents($path . '/index.htm', $contents);
}
/**
* Get list of tables based on InnoDB
*
* @param string $path
*/
private function create_storage_engine($path)
{
$html = '<h1>Table Storage Engines<h1>';
$html .= '<table>';
$html .= '<tr><td valign=top>';
$html .= '<h2>InnoDB</h2>';
foreach($this->innodb as $tablename)
{
$html .= "$tablename<br>";
}
$html .= '<td valign=top>';
$html .= '<h2>Non-InnoDB</h2>';
foreach($this->noninnodb as $tablename)
{
$html .= "$tablename<br>";
}
$html .= '</table>';
file_put_contents($path . '/tablestorage.htm', $html);
}
}
?>