-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.v8.pl
executable file
·1747 lines (1416 loc) · 57.8 KB
/
metrics.v8.pl
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/perl -w
###/mxl/var/perl/bin/perl -w
################################################################################################
#
# Script: Metrics
# By: john eckhardt
# Dated: 12.26.2006
# Purpose: To extract trending metrics. Defined below.
#
# Metric Units Source
# Inbound MTA Load (Unix Load) 900SnapShot
# Outbound MTA Load (Unix Load) 900SnapShot
# Inbound Traffic (Msg / Day) Audit
# Quarantine Traffic (Msg / Day) Audit
# Outbound Traffic (Msg / Day) Audit
# Quarantine DB Size (#) Direct Querry
# SQR Reports Generated (#) RRD
# SQR Reports Finished (time) RRD
# DB Maintenance Finished (time) RRD
################################################################################################
package mxlAgent;
use base 'LWP::UserAgent';
use lib '/usr/local/bin/opsadmin/perl/';
sub get_basic_credentials {
return 'admin', 'or3g0n';
}
package main;
our $author_email = 'John_Vossler\@McAfee.com';
our $script_name = 'metrics.pl';
our $total_metrics = 15;
################################################################################################
### Package Includes
use diagnostics;
use strict;
use warnings;
use Data::Dumper;
use DBI;
use Getopt::Long;
use MIME::Base64;
use MXL::Arch;
use MXL::MXL;
use Net::SMTP;
use POSIX qw(strftime);
use Time::Local;
################################################################################################
### Constants
# Reporting Levels.
use constant MXL_DEBUG => 1;
use constant MXL_TIMING => 2;
use constant MXL_INFO => 3;
use constant MXL_WARN => 4;
use constant MXL_ERROR => 5;
use constant MXL_FATAL => 6;
use constant DAY_SEC => 86400;
################################################################################################
### Variables
my %hshLogTypes = (
MXL_DEBUG, "DEBUG",
MXL_TIMING, "TIMING",
MXL_INFO, "INFO",
MXL_WARN, "WARNING",
MXL_ERROR, "ERROR",
MXL_FATAL, "FATAL ERROR"
);
# EMail.
###my @daily_email = ('[email protected]');
#my @daily_email = ('[email protected]',);
my @debug_email = ('[email protected]');
# DB Variables
my $db_port='5432';
my $db_user='postgres';
my $db_pass='dbG0d';
# Flags.
our ($debug_flag, $email_flag, $verbose_flag) = (0,0,0);
our ($no_db_flag, $no_sqr_flag) = (0,0);
our ($no_inbound_audit_report, $no_outbound_audit_report) = (0,0);
# Storage.
my ($d3_mta_inbound_load, $d3_mta_outbound_load) = (0,0);
my ($l3_mta_inbound_load, $l3_mta_outbound_load) = (0,0);
my ($d3_inbound_mta_count, $l3_inbound_mta_count) = (0,0);
my ($total_mta_inbound_load, $total_mta_outbound_load) = (0,0);
my ($mta_inbound_traffic, $mta_outbound_traffic, $mta_quarantine_traffic) = (0,0,0);
my ($quarantine_size) = (0);
my ($sqr_reports_generated, $sqr_reports_finished_time) = (0,0);
my ($db_maintenience_finished) = (0);
my ($wds_cpl_length, $wds_cpl_size) = (0, 0);
# New Stuff - Archiving.
my%arch_data;
my($arch_num_cust, $arch_num_seats) = (0,0);
my($arch_mesgs_ingested, $arch_msgs_backloged) = (0,0);
my($arch_data393_store_size, $arch_level3_store_size) = (0,0);
my($arch_data393_index_size, $arch_level3_index_size) = (0,0);
my($arch_data393_index_searches, $arch_level3_index_searches) = (0,0);
# New Stuff - WDS
my %wds_data;
my($wds_min_latency, $wds_max_latency) = (0,0);
my($wds_avg_latency, $wds_avg_peak_latency) = (0,0);
my($wds_num_blocked, $wds_num_allowed) = (0,0);
my($wds_num_viruses) = (0);
my($wds_bw_in, $wds_bw_out) = (0,0);
my @results = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
our ($pod) = (0);
our $warnings = "The following warnings were encountered during processing:\n";
# Path and Formats.
our $data_dir = '/usr/local/bin/opsadmin/data/metrics.v8';
#our $daily_audit_report_dir = '/usr/local/bin/opsadmin/data/audit_reports/';
our $daily_audit_report_format = "%s/domain_traffic-%s-daily-pod_%01d.%02d%02d%04d.csv";
our $db_stats_log = '/var/log/mxl/db_stats.log';
our $log_directory = '/usr/local/bin/opsadmin/log';
our $log_file_format = '%04d-%02d-%02d_%02d:%02d:%02d.txt';
#Time.
our $utime = time;
my @ltime = localtime;
my $override_time = '';
my ($year,$month,$day) = ($ltime[5]+1900,$ltime[4]+1,$ltime[3]);
my ($hour,$minute,$second) = ($ltime[2],$ltime[1],$ltime[0]);
our @run_time = ($year,$month,$day,$hour,$minute,$second);
our @smhdmy = ($second,$minute,$hour,$day,$month,$year);
our @ymdhms = ($year,$month,$day,$hour,$minute,$second);
################################################################################################
### Subroutine prototypes
sub check_port($$);
sub count_mtas($$);
sub check_smtp();
sub day_before(@);
sub email_daily_results(@@%%);
sub generate_db_stats_cvs_entry($);
sub get_arch_data();
sub get_log_db_maint_finished_time();
sub get_spam_report_data();
sub get_quar_db_size();
sub get_wds_cpl_data();
sub get_wds_data();
sub logwrite($$);
sub process_900snapshot($$);
sub rev_array(@);
sub querry_log_db_all_mesg_in();
sub querry_log_db_all_mesg_out();
sub querry_log_db_quar_mesg_in();
sub sci_to_int($);
sub summarize_data(@%%);
sub summarize_db_stats($);
sub usage();
sub validate_override_time($);
sub write_daily_data(@%%);
sub write_monthly_data(@%%);
sub ymdhms_human2unix(@);
sub ymdhms_unix2human(@);
################################################################################################
### Begin MAIN process.
# Process Command Line Options or die.
my $result = GetOptions (
"d+" => \$debug_flag, # incrimental
"debug+" => \$debug_flag, # incrimental
"e" => \$email_flag, # flag
"email" => \$email_flag, # flag
"p=i" => \$pod, # numeric
"pod=i" => \$pod, # numeric
"t=i" => \$override_time, # string
"time=i" => \$override_time, # string
"nodb" => \$no_db_flag, # flag
"nosqr" => \$no_sqr_flag, # flag
"v" => \$verbose_flag, # flag
"verbose" => \$verbose_flag # flag
);
unless($result)
{ logwrite(MXL_FATAL, "There was an error interpreting the command line options given."); }
# If this is a debugging effort, don't email the normal recipients or store data in the usual places.
if($debug_flag)
{
@daily_email = @debug_email;
$log_directory = '/usr/local/bin/opsadmin/log/metrics.debug';
$data_dir = '/usr/local/bin/opsadmin/data/metrics.debug';
}
# Open the logfile or die.
my $logfile = $log_directory."/".sprintf($log_file_format, @ymdhms);
open(LOGFILE, ">$logfile")
or logwrite(MXL_FATAL, "$script_name could not open logfile, $logfile. Exiting.\n");
logwrite(MXL_INFO,"$script_name initialized. Running.");
# DBs.
our %dbs = (
"10.$pod.106.130", 'mxl',
"10.$pod.106.131", 'mxl_log',
"10.$pod.106.132", 'mxl_quar',
"10.$pod.106.135", 'mxl_quar',
"10.$pod.106.136", 'mxl_log',
);
#Assure the pod variable is valid and welcome users to the script.
if (($pod != 1) && ($pod != 2)) {
&usage;
logwrite(MXL_FATAL,"The -p flag was not used, or was used improperly. Dying.");
} else {
if(($debug_flag) || ($verbose_flag)) {
print "Welcome to Metrics! (A daily metric gathering script)\n";
print "Processing Denver Pod $pod\n";
}
}
# Timing is everything.
# This script behaves as if it were 9AM yesterday, unless ...
# specifically overidden by command line option above.
# Check for the command line time override and process.
if($override_time) {
my $msg = "An override time of $override_time, has been sumbitted.";
logwrite(MXL_INFO, $msg);
if(($debug_flag) || ($verbose_flag))
{ print "$msg\n"; }
@ymdhms = validate_override_time($override_time);
$msg = "The override time has been accepted. This run is now for @ymdhms\n";
logwrite(MXL_INFO, $msg);
if(($debug_flag) || ($verbose_flag))
{ print "$msg\n"; }
} else {
@ymdhms = day_before(@ymdhms);
@ymdhms = ($ymdhms[0], $ymdhms[1], $ymdhms[2], 9, 0, 0);
my $msg = "Override time was _not_ specified, so the effective time will be 9AM".
" yesterday: ".sprintf("%04d/%02d/%02d %02d:%02d:%02d",@ymdhms). "\n";
if(($debug_flag) || ($verbose_flag))
{ print "$msg\n"; }
logwrite(MXL_INFO, $msg);
}
# Reset the smhdmy and utime now that @ymdhms is officially established.
$utime=timelocal(rev_array(ymdhms_human2unix(@ymdhms)));
@smhdmy = rev_array(@ymdhms);
my $msg = "$script_name has established it's effective run time: ".
sprintf("%04d/%02d/%02d %02d:%02d:%02d", @ymdhms)."\n".
"The equivalent Unix time is, $utime\n";
"Verify that ".(strftime "%a %b %e %H:%M:%S %Y", localtime($utime)).
" is equivalent as well\n";
logwrite(MXL_INFO, $msg);
if(($debug_flag) || ($verbose_flag)) {
print $msg;
}
if($debug_flag){ print "Checkpoint 1\n"; }
################################################################################################
# Inbound Traffic (Msg / Day) Audit Stored in: $mta_inbound_traffic
# Quarantine Traffic (Msg / Day) Audit Stored In: $mta_quarantine_traffic
# Outbound Traffic (Msg / Day) Audit Stored In: $mta_outbound_traffic
unless($no_db_flag) {
($mta_inbound_traffic) = querry_log_db_all_mesg_in();
($mta_quarantine_traffic) = querry_log_db_quar_mesg_in();
($mta_outbound_traffic) = querry_log_db_all_mesg_out();
}
if($debug_flag){ print "Checkpoint 2\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
# Inbound MTA Load (Unix Load) 900SnapShot Stored In: $mta_inbound_load
# Outbound MTA Load (Unix Load) 900SnapShot Stored In: $mta_outbound_load
($d3_mta_inbound_load, $d3_mta_outbound_load) = process_900snapshot('localhost', '/tmp/900snapshot.log');
if($debug_flag){ print "Checkpoint 3\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
# Inbound MTA Load (Unix Load) 900SnapShot Stored In: $mta_inbound_load
# Outbound MTA Load (Unix Load) 900SnapShot Stored In: $mta_outbound_load
($l3_mta_inbound_load, $l3_mta_outbound_load) = process_900snapshot("10.$pod.107.1", '/tmp/900snapshot.log');
if($debug_flag){ print "Checkpoint 4\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
### The number of MTAs in production at any given point in time is somewhat variable.
# Number of Inbound MTAs at D393: $d3_inbound_mta_count
# Number of Inbound MTAs at L3: $l3_inbound_mta_count
($d3_inbound_mta_count) = count_mtas($pod, 106);
if($debug_flag){ print "Checkpoint 4.5\n"; }
($l3_inbound_mta_count) = count_mtas($pod, 107);
if($debug_flag){ print "Checkpoint 5\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
# The totals are simply the sums.
($total_mta_inbound_load, $total_mta_outbound_load) = (($d3_mta_inbound_load+$l3_mta_inbound_load),($d3_mta_outbound_load+$l3_mta_outbound_load));
if($debug_flag){ print "Checkpoint 6\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
### Directly querry the quarantine DB for the number of items in quarantine. ###
# Quarantine DB Size (#) RRD Stored In: $quarantine_size
unless($no_db_flag) {
$quarantine_size = get_quar_db_size();
}
if($debug_flag){ print "Checkpoint 7\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
# Gather the Spam (SQR) Reports' data from RRD records.
# SQR Reports Generated (#) RRD Stored In: $sqr_reports_generated
# SQR Reports Finished (time) RRD Stored In: $sqr_report_finished_time
unless($no_sqr_flag) {
($sqr_reports_generated, $sqr_reports_finished_time) = get_spam_report_data();
}
if($debug_flag){ print "Checkpoint 8\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
# Gather the DB maintenance data from RRD records.
# DB Maintenance Finished (time) RRD Stored In: $db_maintenience_finished
$db_maintenience_finished = get_log_db_maint_finished_time();
if($debug_flag){ print "Checkpoint 9\n"; }
### The MTA Load is already computed by shinto in the file /tmp/900snapshot.log
# Get WDS CPl data
($wds_cpl_length, $wds_cpl_size) = get_wds_cpl_data();
################################################################################################
# Consolidate the results in an array.
@results = (
$l3_mta_inbound_load, # 1
$l3_mta_outbound_load, # 2
$l3_inbound_mta_count, # 3
$d3_mta_inbound_load, # 4
$d3_mta_outbound_load, # 5
$d3_inbound_mta_count, # 6
$total_mta_inbound_load, # 7
$total_mta_outbound_load, # 8
$mta_inbound_traffic, # 9
$mta_quarantine_traffic, #10
$mta_outbound_traffic, #11
$quarantine_size, #12
$sqr_reports_generated, #13
$sqr_reports_finished_time, #14
$db_maintenience_finished, #15
$wds_cpl_length, #16
$wds_cpl_size #17
);
%arch_data = &get_arch_data();
%wds_data = &get_wds_data();
# Write the resulting data to the daily data file.
&write_daily_data(\@results, \%arch_data, \%wds_data);
# Write the resulting data to the monthly data file.
&write_monthly_data(\@results, \%arch_data, \%wds_data);
# Send daily email reports.
if($email_flag) {
&email_daily_results(\@results, \@daily_email, \%arch_data, \%wds_data);
}
if(($debug_flag) || ($verbose_flag)) {
print "\n",&summarize_data(\@results, \%arch_data, \%wds_data),"\n";
logwrite(MXL_INFO, "The following were this run\'s results:\n\n".
&summarize_data(\@results, \%arch_data, \%wds_data)."\n");
}
logwrite(MXL_INFO,"$script_name is finished. Closing.");
close LOGFILE;
### End MAIN processing.
################################################################################################
################################################################################################
### Subroutines
# Subroutine: check_port
# Args: $ip (sting), $port (string)
# Return Value: true / false (is open?)
# Purpose: Given an IP and port, return a boolean (true/false)
# of whether or not this port is open.
sub check_port($$)
{
my ($ip, $port) = @_;
my $sock = new IO::Socket::INET( PeerAddr => $ip,
PeerPort => $port,
Proto => 'tcp',
Timeout=>2
#) || warn "What's up with the sock?\n$!\n";
);
if ($sock) {
return 1;
} else {
#print "No connection allowed to $ip on $port\n";
return 0;
}
undef $sock;
}
# Subroutine: count_inbound_mtas
# Args: $pod (string), $data_center (string)
# Return Value: $count
# Purpose: Given a data center, return the number of operational MTAs (inbound).
sub count_mtas($$)
{
my ($pod, $dc) = @_;
my $count = 0;
my @mtas = `ssh 10.$pod.$dc.1 'cat /root/dist/mail.inbound'`;
#print "ssh 10.$pod.$dc.1 'cat /root/dist/mail.inbound'\n";
foreach my $mta (@mtas) {
chomp($mta);
#print "$mta\n";
if(&check_port($mta, '25')) {
$count++;
}
}
#print "MTA Count: $count\n";
return $count;
}
# Subroutine: day_before
# Args: @ymdhms(array)
# Return Value: @ymdhms(array)
# Purpose: Given a ymdhms array, return the same for one day before.
sub day_before(@)
{
my $subroutine_name = 'day_before';
my ($year, $month, $day, $hour, $minute, $second) = @_;
my @temp = ($year, $month, $day, $hour, $minute, $second);
my @pmet = ();
foreach my $entry (@ymdhms) {
push @pmet, pop @temp;
}
$pmet[4] = $pmet[4]-1;
my $time = timelocal(@pmet);
my $yesterday = $time - DAY_SEC;
my @new_ltime = localtime($yesterday);
my @return_array = ( $new_ltime[5]+1900,$new_ltime[4]+1,$new_ltime[3],
$new_ltime[2],$new_ltime[1],$new_ltime[0]);
return @return_array;
}
# Subroutine: email_daily_results
# Args: @data (array), @recipients (array)
# Return Value: <void>
# Purpose: Email recipients with the daily summary of Metrics.
sub email_daily_results(@@%%)
{
my $subroutine_name = 'email_daily_results';
if($debug_flag)
{ print "DEBUG: Entering $subroutine_name.\n"; }
elsif($verbose_flag)
{ print "Emailing daily metrics to the recipient list.\n"; }
else
{ ; }
my ($ref1, $ref2, $ref3, $ref4) = @_;
my @data = @$ref1;
my @recipients = @$ref2;
my %arch_data = %$ref3;
my %wds_data = %$ref4;
my $smtp = Net::SMTP->new("127.0.0.1", Port=>"25")
|| logwrite(MXL_FATAL,"Subroutine: email_daily_results: could not initiate the smtp session.");
my $attach = get_monthly_data($ymdhms[0], $ymdhms[1]);
my $content =
"From: Metrics <metrics\@mcafee.com>\n" .
"To: Unspecifcied Recipients <john_vossler\@mcafee.com>\n" .
"Subject: Metrics: Daily Output: ".sprintf("%04d-%02d-%02d", @ymdhms).": Denver Pod ".$pod."\n" .
"MIME-Version: 1.0\n" .
"Content-Transfer-Encoding: 7bit\n" .
"Content-Type: multipart/mixed;\n" .
"\tboundary=\"____BOUNDARY____\"\n" .
"\n" .
"This is a multi-part message in MIME format.\n" .
"\n" .
"--____BOUNDARY____\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"\n" .
&summarize_data(\@data, \%arch_data, \%wds_data).
"\n".$warnings."\n".
"\n\nMonthly summary data attached.\n" .
"--____BOUNDARY____\n" .
"Content-Type: text/plain;\n" .
"\tName=\"".sprintf("%04d-%02.csv",@ymdhms)."\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
"\tfilename=\"".sprintf("%04d-%02d.csv",@ymdhms)."\"\n" .
"\n" .
encode_base64($attach) . "\n" .
"--____BOUNDARY____\n";
if($debug_flag){
print "\tSubroutine: email_daily_results: I am about to email :\n";
foreach my $recipient (@recipients) {
print "\t\t$recipient\n";
}
if($debug_flag > 1)
{ print "\tWith the following content:\n$content\n"; }
}
foreach my $recipient (@recipients) {
logwrite(MXL_INFO, "A daily report was emailed to $recipient.");
}
$smtp->mail("metrics\@mcafee.com")
or logwrite(MXL_FATAL, "Subroutine: email_daily_results: SMTP from address rejected.\n".$smtp->code."\n".$smtp->message);
$smtp->recipient(@recipients, { Notify => ['NEVER'], SkipBad => 1 })
or logwrite(MXL_FATAL, "Subroutine: email_daily_results: SMTP recipients rejected\n".$smtp->code."\n".$smtp->message);
$smtp->data()
or logwrite(MXL_FATAL, "Subroutine: email_daily_results: SMTP host denied data.\n".$smtp->code."\n".$smtp->message);
$smtp->datasend($content)
or logwrite(MXL_FATAL, "Subroutine: email_daily_results: SMTP did not accept the content.\n".$smtp->code."\n".$smtp->message);
$smtp->dataend()
or logwrite(MXL_FATAL, "Subroutine: email_daily_results: SMTP rejected message.\n".$smtp->code."\n".$smtp->message);
$smtp->quit()
or logwrite(MXL_FATAL, "Subroutine: email_daily_results: SMTP did not end properly.\n".$smtp->code."\n".$smtp->message);
}
# Subroutine: generate_db_stats_cvs_entry($)
# Args: $db (scalar)
# Return Value: $cvs_entry (scalar)
# Purpose: For each Log DB, grab DB maintenence info, average the time and return.
sub generate_db_stats_cvs_entry($) {
my $subroutine_name = 'generate_db_stats_cvs_entry';
if($debug_flag)
{ print "DEBUG: Entering $subroutine_name.\n"; }
my ($db) = @_;
my @data = ();
my $date_str = sprintf('%02d/%02d/%04d',$ymdhms[1],$ymdhms[2],$ymdhms[0]);
my $expected_data_points = 12;
my $found = 0;
open(DB_STATS, "<$db_stats_log");
my @lines = <DB_STATS>;
close DB_STATS;
foreach my $line (@lines) {
if(($line =~ $db) && ($line =~ $date_str)) {
chomp($line);
@data = split(/,/,$line,$expected_data_points);
if($debug_flag) {
print "Subroutine: $subroutine_name - Found line in $db_stats_log for $db on $date_str.\n";
print "Subroutine: $subroutine_name - $line\n";
print "Subroutine: $subroutine_name - ".join(',', @data)."\n";
}
$found = 1;
}
}
my $return_string = '';
unless($found) {
$return_string = "$dbs{$db},$db,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN";
return $return_string;
}
for(my $ii=0; $ii<$expected_data_points;$ii++) {
if($data[$ii] eq '') { $data[$ii] = 'NaN'; }
}
unless(($data[0] eq $date_str) && ($data[2] eq $db) && ($data[1] eq $dbs{$db})) {
logwrite(MXL_FATAL, "Subroutine: $subroutine_name: Data from file $db_stats_log does not match what is expected.\n");
}
shift @data;
$return_string = join(',', @data);
if($debug_flag) {
print "DEBUG: $subroutine_name: returning $return_string\n";
}
return $return_string;
}
# Subroutine: get_arch_data
# Args none
# Return Value: %arch_data (hash)
# Purpose: Responsible for collecting WDS data and returning it in a hash structure.
sub get_arch_data()
{
# my($arch_num_cust, $arch_num_seats) = (0,0);
# my($arch_mesgs_ingested, $arch_msgs_backloged) = (0,0);
# my($arch_data393_store_size, $arch_level3_store_size) = (0,0);
# my($arch_data393_index_size, $arch_level3_index_size) = (0,0);
# my($arch_data393_index_searches, $arch_level3_index_searches) = (0,0);
my %data = (
num_cust => '-1',
num_seats => '-1',
msgs_ingested => '-1',
msgs_backlogged => '-1',
data393_store_size => '-1',
level3_store_size => -1,
data393_index_size => -1,
level3_index_size => -1,
data393_searches => -1,
level3_searches => -1,
);
my $line = '';
my $log_file = '';
my @parts = {};
# Count Archiving Customers.
my @cids = Arch::cids_from_mail_source();
$data{'num_cust'} = $#cids;
#Count Archiving Seats.
my $seat_count = 0;
foreach my $cid (@cids) {
my $seats = int(MXL::customer_seats_from_cid($cid));
$seat_count = $seat_count + $seats;
}
$data{'num_seats'} = $seat_count;
# Count Messages Archived.
my $ingest_count = 0;
foreach my $cid (@cids) {
my $count = int(`/usr/local/bin/opsadmin/arc_cust_vel.pl --cid $cid`);
#print "Count: $count\n";
$ingest_count = $ingest_count + $count;
}
$data{'msgs_ingested'} = $ingest_count;
# Count Messages Backlogged.
$data{'msgs_backlogged'} = `sqlite3 /tmp/arc.cust.data.db 'select sum(count) from backlog ;'`;
# Get Data393 Store Size
$line = `ssh 10.$pod.106.54 'df /mxl/msg_archive/mas/mnt/data393 -P' | tail -1`;
@parts = split(/\s+/, $line);
$data{'data393_store_size'} = $parts[2];
# Get Level3 Store Size
$line = `ssh 10.$pod.106.54 'df /mxl/msg_archive/mas/mnt/level3 -P' | tail -1`;
@parts = split(/\s+/, $line);
$data{'level3_store_size'} = $parts[2];
# Get Data393 Index Size
$line = `ssh 10.$pod.106.137 'df /var/msg_archive -P | tail -1'`;
@parts = split(/\s+/, $line);
$data{'data393_index_size'} = $parts[2];
# Get Level3 Index Size
$line = `ssh 10.$pod.107.137 'df /var/msg_archive -P | tail -1'`;
@parts = split(/\s+/, $line);
$data{'level3_index_size'} = $parts[2];
# Get Data393 Index Searches
$log_file = sprintf('/mxl/var/jetty/logs/%04d_%02d_%02d.request.log', $ymdhms[0], $ymdhms[1], $ymdhms[2]);
$line = `ssh 10.$pod.106.137 'wc -l $log_file'`;
@parts = split(/\s+/, $line);
$data{'data393_searches'} = $parts[0];
# Get Level3 Index Searches
$log_file = sprintf('/mxl/var/jetty/logs/%04d_%02d_%02d.request.log', $ymdhms[0], $ymdhms[1], $ymdhms[2]);
$line = `ssh 10.$pod.107.137 'wc -l $log_file'`;
@parts = split(/\s+/, $line);
$data{'level3_searches'} = $parts[0];
if($debug_flag){ print Dumper(%data); }
return %data;
}
# Subroutine: get_log_db_maint_finished_time
# Args: <none>
# Return Value: $data_string(scalar)
# Purpose: For each Log DB, grab DB maintenence info, average the time and return.
sub get_log_db_maint_finished_time()
{
my $subroutine_name = 'get_log_db_maint_finished_time';
my $cmd_format = 'ssh %s "zcat /var/log/mxl/rotate/db_maint_mxl_log.log.*.gz; cat /var/log/mxl/db_maint_mxl_log.log"';
my @log_dbs = ( sprintf("10.%d.106.131", $pod), sprintf("10.%d.106.136", $pod) );
my %maint_times = ();
my $today = sprintf("%04d%02d%02d", @ymdhms);
foreach my $log_db (@log_dbs) {
my $cmd = sprintf($cmd_format, $log_db);
my @results = `$cmd`;
unless (@results){
my $warning = "Subroutine: $subroutine_name: I have no results for Log DB $log_db\n";
logwrite(MXL_WARN, $warning);
warn $warning;
$warnings .= $warning;
}
foreach my $result (@results) {
if(($result =~ /$today/) && ($result =~ 'Finish'))
{ $maint_times{$log_db}=substr($result,9,8); print "Maint Time: $maint_times{$log_db}\n"; }
}
}
my $max = 0;
foreach my $key ( keys %maint_times ) {
my $tmp = $maint_times{$key};
$tmp =~ s/://g;
if($tmp > $max)
{ $max = $tmp; }
}
my $str = 'NaN';
if($max > 0)
{ $str = sprintf("%02d:%02d:%02d", substr($max,0,2), substr($max,2,2), substr($max,4,2)); }
return $str;
}
# Subroutine: get_monthly_data
# Args: $year(scalar), $month(scalar)
# Return Value: $data_string(scalar)
# Purpose: Grab the monthly data file's contents and chunk.
sub get_monthly_data($$)
{
my $subroutine_name = 'get_monthly_data';
my ($year, $month) = @_;
open(MONTHLY, "<$data_dir/monthly/".sprintf("%04d-%02d.csv", $year, $month))
or logwrite(MXL_FATAL, "Subroutine: $subroutine_name: Could not open monthly report.\n");
my @monthly = <MONTHLY>;
close MONTHLY;
my $data_string = '';
foreach my $line (@monthly)
{ $data_string .= $line; }
return $data_string;
}
# Subroutine: get_spam_report_data
# Args: <none>
# Return Value: $sqr_reports_generated (scalar), $sqr_reports_finished_time (scalar)
# Purpose: For each Spam Quarantine Report (SQR) machine, grab info on number of reports generated and time completed.
sub get_spam_report_data()
{
my $subroutine_name = 'get_spam_report_data';
my $count_cmd_format = 'ssh %s "zgrep Reports /var/log/mxl/rotate/spam_report.log*gz; grep Reports /var/log/mxl/spam_report.log*"';
my $time_cmd_format = 'ssh %s "zgrep \"*** End run ***\" /var/log/mxl/rotate/spam_report.log*gz;'.
' grep \"*** End run ***\" /var/log/mxl/spam_report.log*"';
my %sqr_report_times = ();
my %sqr_report_counts = ();
my @sqr_boxen = ( sprintf("10.%d.106.61", $pod), sprintf("10.%d.106.62", $pod), sprintf("10.%d.106.63", $pod) );
my $today = strftime "%a %b %e ", localtime($utime);
foreach my $sqr_box (@sqr_boxen) {
my $count_cmd = sprintf($count_cmd_format, $sqr_box);
my @count_results = `$count_cmd`;
my $time_cmd = sprintf($time_cmd_format, $sqr_box);
my @time_results = `$time_cmd`;
unless ((@count_results) && (@time_results)){
my $warning = "Subroutine: $subroutine_name: I have no results for SQR box, $sqr_box.\n";
logwrite(MXL_WARN, $warning);
warn $warning;
$warnings .= $warning;
next;
}
foreach my $count_result (@count_results) {
chomp ($count_result);
if($count_result =~ /$today/) {
my @parts = split(/ /, $count_result);
$sqr_report_counts{$sqr_box} = $parts[$#parts];
}
}
foreach my $time_result (@time_results) {
chomp ($time_result);
if($time_result =~ /$today/) {
my @parts1 = split(/\[/, $time_result);
my @parts2 = split(/\s+/,$parts1[1]);
$sqr_report_times{$sqr_box} = $parts2[3];
}
}
}
my $count = 0;
my $finish_time;
my $max = 0;
foreach my $key ( keys %sqr_report_times ) {
my $tmp = $sqr_report_times{$key};
$tmp =~ s/://g;
if($tmp > $max) {
$max = $tmp;
#$finish_time = sprintf("%04d:%02d:%02d:%s", $ymdhms[0], $ymdhms[1], $ymdhms[2], $sqr_report_times{$key});
$finish_time = $sqr_report_times{$key};
}
}
$count += $_ for values %sqr_report_counts;
unless(($max > 0) && ($count > 0))
{ $max = 'NaN'; $count = 'NaN'; }
return ($count, $finish_time);
}
# Subroutine: get_quar_db_size
# Args:
# Return Value: $quarantine_size(scalar)
# Purpose: Email recipients with the daily summary of Metrics.
sub get_quar_db_size()
{
my $subroutine_name = 'get_quar_db_size';
if($debug_flag)
{ print "DEBUG: Entering $subroutine_name.\n"; }
my $return_val = 'NaN';
my $quar_table_format = 'mxl_quar_%04d%02d%02d';
my $quar_db_ip_format = '10.%d.106.%d';
my @quar_db_suffixes = ('132','135');
my $db_user = 'postgres';
my $db_pass = 'dbG0d';
foreach my $suffix (@quar_db_suffixes) {
my $db_ip = sprintf($quar_db_ip_format, $pod, $suffix);
my $db_source = "dbi:Pg:dbname=mxl_quar;host=$db_ip;port=5432";
my $db_table = sprintf($quar_table_format, @ymdhms);
my $db_querry = "SELECT COUNT(*) from $db_table";
if(($debug_flag) || ($verbose_flag))
{ print "Now querrying $db_ip for quaratine size. This takes several minutes to complete.\n"; }
if($debug_flag){
print "\tSubroutine:$subroutine_name: Connecting to Quar DB at $db_ip\n";
print "\tSubroutine:$subroutine_name: DB Login w/ User: $db_user, Pass: $db_pass\n";
print "\tSubroutine:$subroutine_name: Looking for table: $db_table\n";
print "\tSubroutine:$subroutine_name: DB Source String: $db_source\n";
print "\tSubroutine:$subroutine_name: Sumbitting Querry: $db_querry\n";
}
my $dbh = DBI->connect("$db_source", "$db_user", "$db_pass", { PrintError => 1, RaiseError=> 0, AutoCommit => 0 })
or logwrite(MXL_FATAL, "Subroutine:$subroutine_name: Could not connect to DB, $DBI::errstr");
my $sth = $dbh->prepare("$db_querry")
or logwrite(MXL_WARN, "Subroutine:$subroutine_name: Could not prepare DB querry, $db_querry. $DBI::errstr");
$sth->execute()
or logwrite(MXL_WARN, "Subroutine:$subroutine_name: Could not execute DB querry, $db_querry. $DBI::errstr");
my @row = $sth->fetchrow_array
or logwrite(MXL_WARN, "Subroutine:$subroutine_name: Could fetch the return value. $DBI::errstr");
if($return_val eq 'NaN')
{ $return_val = $row[0]; }
else
{ $return_val = $return_val + $row[0]; }
$sth->finish;
$dbh->disconnect;
}
return $return_val;
}
sub get_wds_cpl_data()
{
my ($length, $size) = ('NaN', 'NaN');
my $cpl_url = "https://10.$pod.64.1:8082/Policy/Current";
print "$cpl_url\n";
my $ua = mxlAgent->new
|| die "Could not create a new user agent.\n$!\n";
$ua->agent("MX Logic Metrics Spider 1.0 ");
# Create a request object.
#my $req = HTTP::Request->new( GET => $cpl_url )
my $res = $ua->get($cpl_url)
|| die "Could not create request object.\n$!\n";
# Pass request to the user agent and get a response.
#my $res = $ua->request($req)
# | die "Our user agent could not execute the page request.\n".$ua->status_line."\n$!\n";
# Check the outcome of the response
if ($res->is_success) {
my $save_file = "/tmp/cpl.html";
open OUTFILE, ">$save_file";
print OUTFILE $res->content;
close OUTFILE;
my @lines = split(/\n/, $res->content);
my @stats = stat($save_file);
$length = $#lines;
$size = $stats[7];
} else {
print $res->status_line, "\n";
}
return ($length, $size);
}
# Subroutine: get_wds_data
# Args none
# Return Value: %wds_data (hash)
# Purpose: Responsible for collecting WDS data and returning it in a hash structure.
sub get_wds_data()
{
my ($length, $size) = ('NaN', 'NaN');
# my($wds_min_latency, $wds_max_latency) = (0,0);
# my($wds_avg_latency, $wds_avg_peak_latency) = (0,0);
# my($wds_num_blocked, $wds_num_allowed) = (0,0);
# my($wds_num_viruses) = (0);
# my($wds_bw_in, $wds_bw_out) = (0,0);
my %data = (
min_latency => '0',
min_between_all => '9999',
max_latency => '0',
max_between_all => '0',
avg_latency => '0',
avg_peak_latency => '0',
reqs_allowed => 0,
reqs_blocked => 0,
viruses => 0,
bw_in => 0,
bw_out => 0,
);
my @proxies = (
"p01c64y064",
"p01c64y065",
"p01c64y066",
"p01c64y067",
#"10.$pod.65.64",
#"10.$pod.65.65",
#"10.$pod.65.66",