forked from poppyred/1.6.x
/
exec.influxdb.php
executable file
·1397 lines (1121 loc) · 45.6 KB
/
exec.influxdb.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
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/php -q
<?php
$GLOBALS["BASEDIR"]="/usr/share/artica-postfix/ressources/interface-cache";
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
$GLOBALS["FORCE"]=false;
$GLOBALS["RECONFIGURE"]=false;
$GLOBALS["SWAPSTATE"]=false;
$GLOBALS["NOSQUIDOUTPUT"]=true;
$GLOBALS["TITLENAME"]="InfluxDB Daemon";
$GLOBALS["PROGRESS"]=false;
$GLOBALS["MIGRATION"]=false;
if(preg_match("#--verbose#",implode(" ",$argv))){$GLOBALS["VERBOSE"]=true;
$GLOBALS["OUTPUT"]=true;$GLOBALS["debug"]=true;ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);ini_set('error_prepend_string',null);ini_set('error_append_string',null);}
if(preg_match("#--output#",implode(" ",$argv))){$GLOBALS["OUTPUT"]=true;}
if(preg_match("#schedule-id=([0-9]+)#",implode(" ",$argv),$re)){$GLOBALS["SCHEDULE_ID"]=$re[1];}
if(preg_match("#--force#",implode(" ",$argv),$re)){$GLOBALS["FORCE"]=true;}
if(preg_match("#--reconfigure#",implode(" ",$argv),$re)){$GLOBALS["RECONFIGURE"]=true;}
if(preg_match("#--migration#",implode(" ",$argv),$re)){$GLOBALS["MIGRATION"]=true;}
$GLOBALS["AS_ROOT"]=true;
include_once(dirname(__FILE__).'/ressources/class.ldap.inc');
include_once(dirname(__FILE__).'/ressources/class.squid.inc');
include_once(dirname(__FILE__).'/ressources/class.mysql.inc');
include_once(dirname(__FILE__).'/framework/class.unix.inc');
include_once(dirname(__FILE__).'/framework/frame.class.inc');
include_once(dirname(__FILE__).'/framework/class.settings.inc');
include_once(dirname(__FILE__).'/ressources/class.os.system.inc');
include_once(dirname(__FILE__).'/ressources/class.system.nics.inc');
include_once(dirname(__FILE__).'/ressources/class.influx.inc');
include_once(dirname(__FILE__).'/ressources/class.ccurl.inc');
$GLOBALS["ARGVS"]=implode(" ",$argv);
if($argv[1]=="--clean"){clean();die();}
if($argv[1]=="--stop"){$GLOBALS["OUTPUT"]=true;stop();die();}
if($argv[1]=="--start"){$GLOBALS["OUTPUT"]=true;start();die();}
if($argv[1]=="--restart"){$GLOBALS["OUTPUT"]=true;restart();die();}
if($argv[1]=="--dump-db"){$GLOBALS["OUTPUT"]=true;dump_dbs();die();}
if($argv[1]=="--create-db"){$GLOBALS["OUTPUT"]=true;create_db();die();}
if($argv[1]=="--interface"){$GLOBALS["OUTPUT"]=true;InterfaceSize();$GLOBALS["DEBUG_INFLUX"]=true;die();}
if($argv[1]=="--InfluxDbSize"){$GLOBALS["OUTPUT"]=true;InfluxDbSize();$GLOBALS["DEBUG_INFLUX"]=true;die();}
if($argv[1]=="--install"){$GLOBALS["OUTPUT"]=true;install($argv[2]);die();}
if($argv[1]=="--remove-db"){$GLOBALS["OUTPUT"]=true;remove_db();die();}
if($argv[1]=="--disable-db"){$GLOBALS["OUTPUT"]=true;disable_db();die();}
if($argv[1]=="--enable-db"){$GLOBALS["OUTPUT"]=true;enable_db();die();}
if($argv[1]=="--initd"){$GLOBALS["OUTPUT"]=true;initd();die();}
if($argv[1]=="--install-progress"){$GLOBALS["OUTPUT"]=true;$GLOBALS["PROGRESS"]=true;install();die();}
if($argv[1]=="--restart-progress"){$GLOBALS["OUTPUT"]=true;$GLOBALS["PROGRESS"]=true;restart_progress();die();}
if($argv[1]=="--backup"){$GLOBALS["OUTPUT"]=true;$GLOBALS["PROGRESS"]=true;backup_database();die();}
if($argv[1]=="--time"){timeZoneUTC();exit;}
if($argv[1]=="--config"){Config();exit;}
if($argv[1]=="--restore"){restore_influx();exit;}
if($argv[1]=="--tests"){tests();exit;}
if($argv[1]=="--restore-server"){restore_influx_remote($argv[2]);exit;}
if($argv[1]=="--refresh-progress"){refresh_progress();exit;}
if($argv[1]=="--InfluxDBPassword"){InfluxDBPassword();exit;}
if($argv[1]=="--remote-progress"){remote_progress();exit;}
if($argv[1]=="--disconnect-progress"){disconnect_progress();exit;}
query_influx($argv[1]);
function restart() {
$unix=new unix();
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
stop(true);
sleep(1);
start(true);
}
function initd(){
include_once(dirname(__FILE__)."/ressources/class.influxdb-service.inc");
$unix=new unix();
$sock=new sockets();
$unix=new unix();
influx_db_service();
echo "Influx: [INFO] Writing /etc/init.d/influx-db with new config\n";
}
function dump_dbs(){
$influx=new influx();
$influx->ROOT_DUMP_ALL_DATABASES();
}
function DebianVersion(){
$ver=trim(@file_get_contents("/etc/debian_version"));
preg_match("#^([0-9]+)\.#",$ver,$re);
if(preg_match("#squeeze\/sid#",$ver)){return 6;}
return $re[1];
}
function install($filekey=0){
include_once(dirname(__FILE__)."/ressources/class.ccurl.inc");
$unix=new unix();
$filename=null;
$MD5=null;
$DebianVersion=DebianVersion();
if($DebianVersion<7){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, influxdb Debian version incompatible!\n";}
build_progress_idb("Incompatible system!",110);
die();
}
if($filekey<>0){
$sock=new sockets();
$ArticaTechNetSquidRepo=unserialize(base64_decode($sock->GET_INFO("ArticaTechNetInfluxRepo")));
$version=$ArticaTechNetSquidRepo[$filekey]["VERSION"];
$filename=$ArticaTechNetSquidRepo[$filekey]["FILENAME"];
$MD5=$ArticaTechNetSquidRepo[$filekey]["MD5"];
}
if($filename==null){
$filename="influxdb-0.9.0.0.tar.gz";
}
$curl=new ccurl("http://mirror.articatech.com/download/InfluxDatabase/$filename");
$tmpdir=$unix->TEMP_DIR();
$php=$unix->LOCATE_PHP5_BIN();
build_progress_idb("{downloading}",1);
$curl->WriteProgress=true;
$curl->ProgressFunction="download_progress";
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: Downloading $filename\n";}
if(!$curl->GetFile("$tmpdir/$filename")){
build_progress_idb("$curl->error",110);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: $curl->error\n";}
while (list ($key, $value) = each ($curl->errors) ){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: $value\n";}
}
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, influxdb unable to install....\n";}
@unlink("$tmpdir/$filename");
return;
}
if($MD5<>null){
$DESTMD5=md5_file("$tmpdir/$filename");
if($DESTMD5<>$MD5){
echo "$DESTMD5<>$MD5\n";
@unlink("$tmpdir/$filename");
build_progress_idb("{install_failed} {corrupted_package}",110);
return;
}
}
build_progress_idb("{stopping_service}",95);
stop(true);
build_progress_idb("{extracting}",96);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, extracting....\n";}
$tar=$unix->find_program("tar");
shell_exec("$tar xvf $tmpdir/$filename -C /");
if($GLOBALS["MIGRATION"]){
$rm=$unix->find_program("rm");
build_progress_idb("{delete_database}...",97);
shell_exec("$rm -rf /home/artica/squid/InfluxDB");
@mkdir("/home/artica/squid/InfluxDB",0755,true);
shell_exec("$rm -rf /etc/artica-postfix/DIRSIZE_MB_CACHE/*");
build_progress_idb("{build_status}...",98);
InfluxDbSize();
}
if($GLOBALS["PROGRESS"]){
build_progress_idb("{starting_service}",98);
start(true);
}
if($GLOBALS["MIGRATION"]){
build_progress_idb("{restarting_service}",98);
stop(true);
start(true);
}
build_progress_idb("{refresh_status}",98);
shell_exec("$php /usr/share/artica-postfix/exec.squid.interface-size.php --force");
system("/etc/init.d/squid-tail restart");
build_progress_idb("{done}",100);
}
function download_progress( $download_size, $downloaded_size, $upload_size, $uploaded_size ){
if(!isset($GLOBALS["previousProgress"])){$GLOBALS["previousProgress"]= 0;}
if ( $download_size == 0 ){
$progress = 0;
}else{
$progress = round( $downloaded_size * 100 / $download_size );
}
if ( $progress > $GLOBALS["previousProgress"]){
if($progress<95){
build_progress_idb("{downloading}",$progress);
}
$GLOBALS["previousProgress"]=$progress;
}
}
function restart_progress(){
$unix=new unix();
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
build_progress_rs("{stopping_service}",10);
if(!stop(true)){return;}
sleep(1);
build_progress_rs("{starting_service}",30);
shell_exec("/etc/init.d/influx-db start");
$pid=PID_NUM();
if(!$unix->process_exists($pid)){
build_progress_rs("{failed_to_start_service} ",110);
return;
}
build_progress_rs("{starting_service}",70);
system("/etc/init.d/hypercache-tail restart");
build_progress_rs("{starting_service}",80);
system("/etc/init.d/ufdb-tail restart restart");
build_progress_rs("{starting_service}",80);
system("/etc/init.d/squid-tail restart restart");
build_progress_rs("{starting_service} {success}",100);
}
function refresh_progress(){
$unix=new unix();
$sock=new sockets();
build_progress_rs("{database_size}...",25);
InfluxDbSize();
build_progress_rs("{backup_size}...",25);
$InFluxBackupDatabaseDir=$sock->GET_INFO("InFluxBackupDatabaseDir");
if($InFluxBackupDatabaseDir==null){$InFluxBackupDatabaseDir="/home/artica/influx/backup";}
echo "Checking $InFluxBackupDatabaseDir\n";
@mkdir($InFluxBackupDatabaseDir,0755,true);
$size=$unix->DIRSIZE_BYTES_NOCACHE($InFluxBackupDatabaseDir);
@file_put_contents("{$GLOBALS["BASEDIR"]}/influxdb_snapshotsize", $size);
$q=new influx();
build_progress_rs("{last_date}...",50);
$data=$q->QUERY_SQL("SELECT MAX(ZDATE) as MAX FROM access_log");
$date_end=$data[0]->MAX;
echo "* * * END TO {$date_end} ". date("Y-m-d H:i:s",$date_end)."\n";
build_progress_rs("{date_start}...",90);
$data=$q->QUERY_SQL("SELECT MIN(ZDATE) as MIN FROM access_log");
$date_start=$data[0]->MIN;
echo "* * * START FROM {$date_start} ". date("Y-m-d H:i:s",$date_start)."\n";
@file_put_contents("{$GLOBALS["BASEDIR"]}/DATE_START",$date_start);
@file_put_contents("{$GLOBALS["BASEDIR"]}/DATE_END",$date_end);
build_progress_rs("{done}...",100);
}
function restore_influx_remote($server){
$unix=new unix();
$sock=new sockets();
build_progress_rs("{restore} $server",25);
$rm=$unix->find_program("rm");
$fp = @stream_socket_client("tcp://$server:8089",$errno, $errstr,3, STREAM_CLIENT_CONNECT);
if(!$fp){
echo "Error $errno $errstr\n";
build_progress_rs("{restore} $server:8089 {failed}",110);
return false;
}
if (!is_resource($fp)) {
echo "Error $errno $errstr\n";
build_progress_rs("{restore} $server:8089 {failed}",110);
return false;
}
@socket_close($fp);
@fclose($fp);
build_progress_rs("{backup} $server:8089 {please_wait}",30);
$InFluxBackupDatabaseDir=$sock->GET_INFO("InFluxBackupDatabaseDir");
if($InFluxBackupDatabaseDir==null){$InFluxBackupDatabaseDir="/home/artica/influx/backup";}
$InFluxBackupDatabaseDir=$InFluxBackupDatabaseDir."/restore";
if(is_dir($InFluxBackupDatabaseDir)){shell_exec("$rm -rf $InFluxBackupDatabaseDir");}
@mkdir("$InFluxBackupDatabaseDir",0755,true);
$cmd="/opt/influxdb/influxd backup -host {$server}:8089 $InFluxBackupDatabaseDir/snapshot.db";
echo $cmd."\n";
system("$cmd");
if(!is_file("$InFluxBackupDatabaseDir/snapshot.db")){
build_progress_rs("{backup} $server:8089 {failed}",110);
return;
}
$size=@filesize("$InFluxBackupDatabaseDir/snapshot.db");
$size=$size/1024;
$size=$size/1024;
$size=round($size,2);
build_progress_rs("{restore} $server:8089 ($size MB) {please_wait}",50);
$cmd="/opt/influxdb/influxd restore -config /etc/opt/influxdb/influxdb.conf $InFluxBackupDatabaseDir/snapshot.db";
build_progress_rs("{restore}",50);
system($cmd);
build_progress_rs("{restore}",90);
InfluxDbSize();
if(is_dir($InFluxBackupDatabaseDir)){shell_exec("$rm -rf $InFluxBackupDatabaseDir");}
build_progress_rs("{done}",100);
}
function restore_influx(){
$PathToRestore=null;
build_progress_rs("{restore}",25);
$sock=new sockets();
$dataX=unserialize($sock->GET_INFO("InfluxDBRestoreArray"));
while (list ($path, $size) = each ($dataX)){
$basename=basename($path);
if($basename=="snapshot.db"){$PathToRestore=$path;break;}
$dirname=dirname($path);
}
if(!is_file($PathToRestore)){
build_progress_rs("{failed} snapshot.db not found",110);
return;
}
$cmd="/opt/influxdb/influxd restore -config /etc/opt/influxdb/influxdb.conf $PathToRestore";
build_progress_rs("{restore}",50);
system($cmd);
build_progress_rs("{restore}",90);
InfluxDbSize();
build_progress_rs("{done}",100);
}
function timeZoneUTC(){
$UTC_TIME=strtotime(gmdate("Y-m-d H:i:s"));
$CURRENTIME=time();
if($CURRENTIME>$UTC_TIME){
$diff=$CURRENTIME-$UTC_TIME;
$sep="+";
}
if($UTC_TIME>$CURRENTIME){
$diff=$UTC_TIME-$CURRENTIME;
$sep="-";
}
echo "Current: $CURRENTIME UTC:$UTC_TIME Diff=$sep$diff seconds\n";
echo "Strtotime: strtotime(".gmdate("Y-m-d H:i:s")." $sep$diff seconds)\n";
echo date("Y-m-d H:i:s",strtotime(gmdate("Y-m-d H:i:s")." $sep$diff seconds"))."\n";
}
function ifInitScript(){
$f=explode("\n",@file_get_contents("/etc/init.d/influx-db"));
while (list ($filepath, $value) = each ($f) ){
if(preg_match("#OPEN_FILE_LIMIT#", $value)){return true;}
}
}
function start($aspid=false){
$unix=new unix();
$sock=new sockets();
$Masterbin="/opt/influxdb/influxd";
if(!$aspid){
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
}
build_progress_rs("{starting_service}",30);
if(!is_file($Masterbin)){
Install();
if(!is_file($Masterbin)){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, influxdb not installed\n";}
build_progress_rs("{failed_to_start_service} ({not_installed})",110);
return;
}
}
if(!is_file("/etc/artica-postfix/settings/Daemons/EnableInfluxDB")){@file_put_contents("/etc/artica-postfix/settings/Daemons/EnableInfluxDB", 1);}
$pid=PID_NUM();
$SquidPerformance=intval($sock->GET_INFO("SquidPerformance"));
$EnableInfluxDB=intval($sock->GET_INFO("EnableInfluxDB"));
$InfluxUseRemote=intval($sock->GET_INFO("InfluxUseRemote"));
if($InfluxUseRemote==1){$EnableInfluxDB=0;}
$EnableIntelCeleron=intval(file_get_contents("/etc/artica-postfix/settings/Daemons/EnableIntelCeleron"));
if($EnableIntelCeleron==1){$EnableInflux=0;}
if(is_file("/etc/artica-postfix/STATS_APPLIANCE")){$EnableInflux=1;$SquidPerformance=0;$EnableIntelCeleron=0;}
@mkdir("/home/artica/squid/InfluxDB",0755,true);
if($unix->process_exists($pid)){
$timepid=$unix->PROCCESS_TIME_MIN($pid);
if($SquidPerformance>2){stop(true);
build_progress_rs("{starting_service} {failed} ({disabled})",110);
return;
}
if($EnableInfluxDB==0){
stop(true);
build_progress_rs("{starting_service} {failed} ({disabled})",110);
return;
}
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Service already started $pid since {$timepid}Mn...\n";}
build_progress_rs("{already_running}",100);
return true;
}
if($EnableInfluxDB==0){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} InfluxDB engine is disabled\n";}
build_progress_rs("{starting_service} {failed} ({disabled})",110);
return false;
}
if($SquidPerformance>2){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Perfomance is set to no statistics\n";}
build_progress_rs("{starting_service} {failed} ({disabled})",110);
return false;
}
$php5=$unix->LOCATE_PHP5_BIN();
$sysctl=$unix->find_program("sysctl");
$echo=$unix->find_program("echo");
$nohup=$unix->find_program("nohup");
$date=$unix->find_program("date");
if(!is_dir("/var/log/influxdb")){@mkdir("/var/log/influxdb",0755,true);}
$influxdb_version=influxdb_version();
build_progress_rs("{starting_service} v{$influxdb_version}",35);
Config($influxdb_version);
build_progress_rs("{starting_service}",45);
$unix->KILL_PROCESSES_BY_PORT(8086);
if(!ifInitScript()){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} patching init script\n";}
shell_exec("$php5 /usr/share/artica-postfix/exec.initslapd.php --influx");
}
$cmd="/etc/init.d/influx-db start";
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} v$influxdb_version service\n";}
if($GLOBALS["VERBOSE"]){echo "$cmd\n";}
shell_exec($cmd);
for($i=1;$i<5;$i++){
build_progress_rs("{starting_service}",45+$i);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} waiting $i/5\n";}
sleep(1);
$pid=PID_NUM();
if($unix->process_exists($pid)){break;}
}
$pid=PID_NUM();
if($unix->process_exists($pid)){
build_progress_rs("{starting_service}",50);
for($i=1;$i<5;$i++){
build_progress_rs("{starting_service} {waiting_listen_port}",50+$i);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} waiting 8086 port... $i/5\n";}
sleep(1);
$pid=PID_NUM();
if(test_listen_port()){break;}
}
if(!test_listen_port()){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Failed\n";}
build_progress_rs("{failed_to_start_service}",110);
return false;
}
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Success v$influxdb_version PID $pid\n";}
build_progress_rs("{starting_service} {success}",100);
return true;
}else{
build_progress_rs("{failed_to_start_service}",110);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Failed\n";}
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} $cmd\n";}
return false;
}
}
function test_listen_port(){
if(!isset($GLOBALS["InfluxApiIP"])){$GLOBALS["InfluxApiIP"]=@file_get_contents("/etc/artica-postfix/settings/Daemons/InfluxApiIP");}
if($GLOBALS["InfluxApiIP"]==null){$GLOBALS["InfluxApiIP"]="127.0.0.1";}
$fp = @stream_socket_client("tcp://{$GLOBALS["InfluxApiIP"]}:8086",
$errno, $errstr,3, STREAM_CLIENT_CONNECT);
if(!$fp){
if(is_resource($fp)){@socket_close($fp);}
return false;
}
if(is_resource($fp)){@socket_close($fp);}
return true;
}
function stop($aspid=false){
$unix=new unix();
if(!$aspid){
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
}
$pid=PID_NUM();
if(!$unix->process_exists($pid)){
build_progress_rs("{stopping_service}",30);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service already stopped...\n";}
return true;
}
$nohup=$unix->find_program("nohup");
$php5=$unix->LOCATE_PHP5_BIN();
$kill=$unix->find_program("kill");
build_progress_rs("{stopping_service}",15);
if(!ifInitScript()){shell_exec("$php5 /usr/share/artica-postfix/exec.initslapd.php --influx");}
$cmd="/etc/init.d/influx-db stop";
if($GLOBALS["VERBOSE"]){echo "$cmd\n";}
shell_exec($cmd);
$pid=PID_NUM();
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service Shutdown pid $pid...\n";}
unix_system_kill($pid);
for($i=0;$i<5;$i++){
build_progress_rs("{stopping_service}",15+$i);
$pid=PID_NUM();
if(!$unix->process_exists($pid)){break;}
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service waiting pid:$pid $i/5...\n";}
sleep(1);
}
if(test_listen_port()){
for($i=0;$i<5;$i++){
build_progress_rs("{stopping_service} {waiting_port_to_be_closed}",25);
if(!test_listen_port()){break;}
if(!$unix->process_exists($pid)){break;}
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service waiting socket $i/5...\n";}
sleep(1);
}
}
$pid=PID_NUM();
if(!$unix->process_exists($pid)){
build_progress_rs("{stopping_service}",30);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service success...\n";}
return true;
}
build_progress_rs("{stopping_service}",30);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service shutdown - force - pid $pid...\n";}
unix_system_kill_force($pid);
for($i=0;$i<5;$i++){
build_progress_rs("{stopping_service}",30+$i);
$pid=PID_NUM();
if(!$unix->process_exists($pid)){break;}
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service waiting pid:$pid $i/5...\n";}
sleep(1);
}
if($unix->process_exists($pid)){
build_progress_rs("{stopping_service} {failed}",110);
squid_admin_mysql(0, "Failed to start Statistics Engine",__FILE__,__LINE__);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service failed...\n";}
return;
}
return true;
}
function GetInfluxListenIP(){
$unix=new unix();
$sock=new sockets();
$STATS_APPLIANCE=false;
if(is_file("/etc/artica-postfix/STATS_APPLIANCE")){$STATS_APPLIANCE=true;}
$InfluxListenInterface=$sock->GET_INFO("InfluxListenInterface");
if($STATS_APPLIANCE){
if($InfluxListenInterface==null){$InfluxListenInterface="ALL";}
}
if($InfluxListenInterface==null){$InfluxListenInterface="lo";}
if($InfluxListenInterface=="lo"){
$InfluxListenIP="127.0.0.1";
$InfluxApiIP="127.0.0.1";
}
if($InfluxListenInterface=="ALL"){
$InfluxListenIP="0.0.0.0";
$InfluxApiIP="127.0.0.1";
}
if($InfluxListenIP==null){
$unix=new unix();
$NETWORK_ALL_INTERFACES=$unix->NETWORK_ALL_INTERFACES();
$InfluxListenIP=$NETWORK_ALL_INTERFACES[$InfluxListenInterface]["IPADDR"];
$InfluxApiIP=$InfluxListenIP;
if($InfluxListenIP=="0.0.0.0"){$InfluxApiIP="127.0.0.1";}
if($InfluxListenIP=="127.0.0.1"){$InfluxApiIP="127.0.0.1";}
}
if($STATS_APPLIANCE){
if($InfluxListenIP=="127.0.0.1"){$InfluxListenIP="0.0.0.0";}
}
$sock->SET_INFO("InfluxListenIP", $InfluxListenIP);
return $InfluxListenIP;
}
function Config($influxdb_version=null){
$unix=new unix();
$php=$unix->LOCATE_PHP5_BIN();
$sock=new sockets();
@mkdir("/etc/opt/influxdb",0755,true);
@mkdir("/home/artica/squid/InfluxDB/logs",0755,true);
@mkdir("/home/artica/squid/InfluxDB/raft",0755,true);
@mkdir("/home/artica/squid/InfluxDB/db",0755,true);
$InfluxListenIP=null;
$InFluxBackupDatabaseDir=$sock->GET_INFO("InFluxBackupDatabaseDir");
if($InFluxBackupDatabaseDir==null){$InFluxBackupDatabaseDir="/home/artica/influx/backup";}
$InFluxBackupDatabaseMaxContainers=intval("InFluxBackupDatabaseMaxContainers");
if($InFluxBackupDatabaseMaxContainers==0){$InFluxBackupDatabaseMaxContainers=5;}
$InFluxBackupDatabaseInterval=intval("InFluxBackupDatabaseInterval");
if($InFluxBackupDatabaseInterval==0){$InFluxBackupDatabaseInterval=10080;}
if($InFluxBackupDatabaseInterval<1440){$InFluxBackupDatabaseInterval=1440;}
$InfluxListenInterface=$sock->GET_INFO("InfluxListenInterface");
$STATS_APPLIANCE=false;
$Intervals[60]="45 * * * * *";
$Intervals[120]="45 0,2,4,6,8,10,12,14,16,18,20,22 * * *";
$Intervals[240]="45 0,4,6,10,14,18,22 * * *";
$Intervals[1440]="10 1 * * *";
$CRON[]="MAILTO=\"\"";
$CRON[]="{$Intervals[$InFluxBackupDatabaseInterval]} root $php ".__FILE__." --backup >/dev/null 2>&1";
$CRON[]="";
file_put_contents("/etc/cron.d/InfluxBackup",@implode("\n", $CRON));
$CRON=array();
chmod("/etc/cron.d/InfluxBackup",0640);
chown("/etc/cron.d/InfluxBackup","root");
system("/etc/init.d/cron reload");
if(is_file("/etc/artica-postfix/STATS_APPLIANCE")){$STATS_APPLIANCE=true;}
if($STATS_APPLIANCE){
if($InfluxListenInterface==null){$InfluxListenInterface="ALL";}
}
if($InfluxListenInterface==null){$InfluxListenInterface="lo";}
$influxdb_version=influxdb_version();
if(preg_match("#^([0-9]+)\.([0-9]+)\.([0-9]+)#", $influxdb_version,$re)){
$MAJOR=$re[1];
$MINOR=$re[2];
$REV=$re[3];
}
$AS_092=false;
$AS_093=false;
if($MAJOR==0){
if($MINOR==9){
if($REV==1){
$AS_092=true;
}
if($REV==2){
$AS_092=true;
}
if($REV>=3){
$AS_092=false;
$AS_093=true;
}
}
}
$InfluxListenIP=GetInfluxListenIP();
$InfluxApiIP=$InfluxListenIP;
if($InfluxListenIP=="0.0.0.0"){$InfluxApiIP="127.0.0.1";}
if($InfluxListenIP=="127.0.0.1"){$InfluxApiIP="127.0.0.1";}
$sock->SET_INFO("InfluxApiIP", $InfluxApiIP);
$InfluxAdminDisabled=intval($sock->GET_INFO("InfluxAdminDisabled"));
$InfluxAdminPort=intval($sock->GET_INFO("InfluxAdminPort"));
if($InfluxAdminPort==0){$InfluxAdminPort=8083;}
$unix=new unix();
@mkdir("/home/artica/squid/InfluxDB",0755,true);
@mkdir("/home/artica/squid/InfluxDB/db",0755,true);
@mkdir("/home/artica/squid/InfluxDB/raft",0755,true);
@mkdir("/home/artica/squid/InfluxDB/meta",0755,true);
@mkdir("/home/artica/squid/InfluxDB/hh",0755,true);
$InfluxListenHosname=$InfluxListenIP;
if($InfluxListenHosname=="0.0.0.0"){$InfluxListenHosname=$unix->hostname_g();}
$f[]="# Created on ". date("Y-m-d H:i:s");
$f[]="# Welcome to the InfluxDB configuration file.";
$f[]="# Listen interface $InfluxListenInterface/$InfluxListenIP/$InfluxApiIP.";
$f[]="";
$f[]="# If hostname (on the OS) doesn't return a name that can be resolved by the other";
$f[]="# systems in the cluster, you'll have to set the hostname to an IP or something";
$f[]="# that can be resolved here.";
$f[]="hostname = \"$InfluxListenHosname\"";
$f[]="bind-address = \"$InfluxListenIP\"";
$f[]="";
$f[]="# The default cluster and API port";
$f[]="port = 8086";
$f[]="";
$f[]="# Once every 24 hours InfluxDB will report anonymous data to m.influxdb.com";
$f[]="# The data includes raft id (random 8 bytes), os, arch and version";
$f[]="# We don't track ip addresses of servers reporting. This is only used";
$f[]="# to track the number of instances running and the versions, which";
$f[]="# is very helpful for us.";
$f[]="# Change this option to true to disable reporting.";
$f[]="reporting-disabled = true";
$f[]="";
$f[]="# Controls settings for initial start-up. Once a node is successfully started,";
$f[]="# these settings are ignored. If a node is started with the -join flag,";
$f[]="[initialization]";
$f[]="join-urls = \"\" # Comma-delimited URLs, in the form http://host:port, for joining another cluster.";
$f[]="";
$f[]="# Control authentication";
$f[]="# If not set authetication is DISABLED. Be sure to explicitly set this flag to";
$f[]="# true if you want authentication.";
$f[]="[authentication]";
$f[]="enabled = false";
$f[]="";
$f[]="";
$f[]="[meta]";
$f[]=" dir = \"/home/artica/squid/InfluxDB/meta\"";
$f[]=" hostname = \"$InfluxListenHosname\"";
$f[]=" bind-address = \":8089\"";
$f[]=" retention-autocreate = true";
$f[]=" election-timeout = \"1s\"";
$f[]=" heartbeat-timeout = \"1s\"";
$f[]=" leader-lease-timeout = \"500ms\"";
$f[]=" commit-timeout = \"50ms\"";
$f[]="";
$f[]="";
$f[]="[hinted-handoff]";
$f[]=" enabled = true";
$f[]=" dir = \"/home/artica/squid/InfluxDB/hh\"";
$f[]=" max-size = 1073741824";
$f[]=" max-age = \"168h0m0s\"";
$f[]=" retry-rate-limit = 0";
$f[]=" retry-interval = \"1s\"";
$f[]="";
$f[]="# Configure the admin server";
$f[]="[admin]";
//$f[]="auth-enabled=true";
if($InfluxAdminDisabled==0){
$f[]="enabled = true";
$f[]="port = $InfluxAdminPort";
}else{
$f[]="enabled = false";
$f[]="#port = $InfluxAdminPort";
}
$f[]="";
$f[]="# Configure the HTTP API endpoint. All time-series data and queries uses this endpoint.";
$f[]="[api]";
$f[]="# ssl-port = 8087 # SSL support is enabled if you set a port and cert";
$f[]="# ssl-cert = \"/path/to/cert.pem\"";
$f[]="";
$f[]="# Configure the Graphite plugins.";
$f[]="[[graphite]] # 1 or more of these sections may be present.";
$f[]="enabled = false";
$f[]="# protocol = \"\" # Set to \"tcp\" or \"udp\"";
$f[]="# address = \"0.0.0.0\" # If not set, is actually set to bind-address.";
$f[]="# port = 2003";
$f[]="# name-position = \"last\"";
$f[]="# name-separator = \"-\"";
$f[]="# database = \"\" # store graphite data in this database";
$f[]="";
$f[]="# Configure the collectd input.";
$f[]="[collectd]";
$f[]="enabled = false";
$f[]="#address = \"0.0.0.0\" # If not set, is actually set to bind-address.";
$f[]="#port = 25827";
$f[]="#database = \"collectd_database\"";
$f[]="#typesdb = \"types.db\"";
$f[]="";
$f[]="# Configure the OpenTSDB input.";
$f[]="[opentsdb]";
$f[]="enabled = false";
$f[]="#address = \"0.0.0.0\" # If not set, is actually set to bind-address.";
$f[]="#port = 4242";
$f[]="#database = \"opentsdb_database\"";
$f[]="";
$f[]="# Configure UDP listener for series data.";
if($AS_093){
$f[]="[[udp]]";
}else{
$f[]="[udp]";
}
$f[]="enabled = false";
$f[]="#bind-address = \"0.0.0.0\"";
$f[]="#port = 4444";
$f[]="";
$f[]="# Broker configuration. Brokers are nodes which participate in distributed";
$f[]="# consensus.";
$f[]="[broker]";
$f[]="enabled = true";
$f[]="# Where the Raft logs are stored. The user running InfluxDB will need read/write access.";
$f[]="dir = \"/home/artica/squid/InfluxDB/raft\"";
$f[]="truncation-interval = \"10m\"";
$f[]="max-topic-size = 52428800";
$f[]="max-segment-size = 10485760";
$f[]="";
$f[]="# Raft configuration. Controls the distributed consensus system.";
$f[]="[raft]";
$f[]="apply-interval = \"10ms\"";
$f[]="election-timeout = \"5s\"";
$f[]="heartbeat-interval = \"100ms\"";
$f[]="reconnect-timeout = \"10ms\"";
$f[]="";
$f[]="# Data node configuration. Data nodes are where the time-series data, in the form of";
$f[]="# shards, is stored.";
$f[]="[data]";
$f[]="enabled = true";
$f[]="dir = \"/home/artica/squid/InfluxDB/db\"";
if($AS_093){
$f[]="# The following WAL settings are for the b1 storage engine used in 0.9.2. They won't";
$f[]="# apply to any new shards created after upgrading to a version > 0.9.3.";
$f[]="max-wal-size = 104857600 # Maximum size the WAL can reach before a flush. Defaults to 100MB.";
$f[]="wal-flush-interval = \"10m\" # Maximum time data can sit in WAL before a flush.";
$f[]="wal-partition-flush-delay = \"2s\" # The delay time between each WAL partition being flushed.";
$f[]="";
if(!is_dir("/home/artica/squid/InfluxDB/wal")){@mkdir("/home/artica/squid/InfluxDB/wal",0755,true);}
$f[]="# These are the WAL settings for the storage engine >= 0.9.3";
$f[]="wal-dir = \"/home/artica/squid/InfluxDB/wal\"";
$f[]="wal-enable-logging = true";
$f[]="";
$f[]="# When a series in the WAL in-memory cache reaches this size in bytes it is marked as ready to";
$f[]="# flush to the index";
$f[]="wal-ready-series-size = 4096";
$f[]="";
$f[]="# Flush and compact a partition once this ratio of series are over the ready size";
$f[]="# wal-compaction-threshold = 0.6";
$f[]="";
$f[]="# Force a flush and compaction if any series in a partition gets above this size in bytes";
$f[]="# wal-max-series-size = 2097152";
$f[]="";
$f[]="# Force a flush of all series and full compaction if there have been no writes in this";
$f[]="# amount of time. This is useful for ensuring that shards that are cold for writes don't";
$f[]="# keep a bunch of data cached in memory and in the WAL.";
$f[]="# wal-flush-cold-interval = \"10m\"";
$f[]="";
$f[]="# Force a partition to flush its largest series if it reaches this approximate size in";
$f[]="# bytes. Remember there are 5 partitions so you'll need at least 5x this amount of memory.";
$f[]="# The more memory you have, the bigger this can be.";
$f[]="wal-partition-size-threshold = 10485760";
}
$f[]="";
$f[]="# Auto-create a retention policy when a database is created. Defaults to true.";
$f[]="retention-auto-create = true";
$f[]="";
$f[]="# Control whether retention policies are enforced and how long the system waits between";
$f[]="# enforcing those policies.";
$f[]="retention-check-enabled = true";
$f[]="retention-check-period = \"10m\"";
$f[]="";
$f[]="# Configuration for snapshot endpoint.";
$f[]="[snapshot]";
$f[]="enabled = true # Enabled by default if not set.";
$f[]="";
$f[]="[logging]";
$f[]="write-tracing = false # If true, enables detailed logging of the write system.";
$f[]="raft-tracing = false # If true, enables detailed logging of Raft consensus.";
$f[]="http-access = false # If true, logs each HTTP access to the system.";
$f[]="";
$f[]="# InfluxDB can store statistical and diagnostic information about itself. This is useful for";
$f[]="# monitoring purposes. This feature is disabled by default, but if enabled, these data can be";
$f[]="# queried like any other data.";
$f[]="[monitoring]";
$f[]="enabled = false";
$f[]="write-interval = \"1m\" # Period between writing the data.\n";
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Saving settings in 0.9x mode\n";}
build_progress_rs("{starting_service} {configuration_done}",40);
@file_put_contents("/etc/opt/influxdb/influxdb.conf", @implode("\n", $f));
}
function backup_database(){
$unix=new unix();
$q=new mysql();
$sock=new sockets();
$InFluxBackupDatabaseDir=$sock->GET_INFO("InFluxBackupDatabaseDir");
if($InFluxBackupDatabaseDir==null){
$InFluxBackupDatabaseDir="/home/artica/influx/backup";
}
$InFluxBackupDatabaseMaxContainers=intval("InFluxBackupDatabaseMaxContainers");
if($InFluxBackupDatabaseMaxContainers==0){$InFluxBackupDatabaseMaxContainers=5;}
$MaxContainers=5;
@mkdir($InFluxBackupDatabaseDir,0755,true);
$GetInfluxListenIP=GetInfluxListenIP();
if($GetInfluxListenIP=="0.0.0.0"){$GetInfluxListenIP="127.0.0.1";}
build_progress_idb("{backup} - $GetInfluxListenIP",5);
$tar=$unix->find_program("tar");
@chdir("/home/artica/squid/InfluxDB");
system("cd /home/artica/squid/InfluxDB");
$time=time();
$rm=$unix->find_program("rm");
build_progress_idb("{backup}",50);
$cmd="/opt/influxdb/influxd backup -host {$GetInfluxListenIP}:8089 $InFluxBackupDatabaseDir/snapshot.db";
echo $cmd."\n";
system("$cmd");