This file is indexed.

/usr/share/munin/plugins/zimbra_ is in munin-node 1.4.6-3ubuntu3.4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl
# -*- cperl -*-
#
# Copyright (c) 2009 Erik Inge Bolsø, Redpill Linpro AS
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.

=pod

=encoding UTF-8

=head1 NAME

zimbra_ - Plugin to monitor all sorts of Zimbra statistics

=head1 APPLICABLE SYSTEMS

Zimbra 5+ with zmstat statistics enabled.

Text::CSV_XS (perl-Text-CSV_XS from EPEL, for example)

=head1 CONFIGURATION

Add this to the relevant munin-node config file:

 [zimbra*]
 group zimbra

The following environment variables are used by this plugin:

=over 4

=item csvpath

Path to zmstat csv files (default /opt/zimbra/zmstat)

=back

=head1 BUGS/GOTCHAS

Only reads latest value from the csv, which may be a "last 30 seconds"
or "last minute" value, so not a representative "last five minutes" value.
The original Zimbra stat charts often use a moving average of some sort.

=head1 AUTHOR

Erik Inge Bolsø <knan@redpill-linpro.com>, Redpill Linpro AS

=head1 COPYRIGHT

Copyright (c) 2009 Erik Inge Bolsø, Redpill Linpro AS

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; version 2
dated June, 1991.

=head1 VERSION 

  $Id$

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf suggest

=cut

use strict;
use warnings;

use Fcntl;
use Tie::File;

my $ret = "";

if (!eval "require Text::CSV_XS;") {
        $ret = "No Text::CSV_XS";
}

my $csvpath = ($ENV{'csvpath'} || '/opt/zimbra/zmstat');

# The possible measurements
my %ops =
    (
     'client_active_connections'
     => {
         'vlabel'   => 'connections',
         'title'    => "Client active connections",
         'info'     => "Mailboxd Active Connections by Client Protocol according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'imap',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'imap_conn',
           },
           {
           'label'    => 'imap_ssl',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'imap_ssl_conn',
           },
           {
           'label'    => 'pop3',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'pop_conn',
           },
           {
           'label'    => 'pop3_ssl',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'pop_ssl_conn',
           }, ],
        },
     'client_request_rate'
     => {
         'vlabel'   => 'requests/min',
         'title'    => "Client request rate",
         'info'     => "Mailboxd Request Rate by Client Protocol according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'soap',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'soap_count',
           },
           {
           'label'    => 'imap',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'imap_count',
           },
           {
           'label'    => 'pop3',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'pop_count',
           }, ],
        },
     'client_response_time'
     => {
         'vlabel'   => 'ms',
         'title'    => "Client response time",
         'info'     => "Mailboxd Response Time by Client Protocol according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'soap',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'soap_ms_avg',
           },
           {
           'label'    => 'imap',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'imap_ms_avg',
           },
           {
           'label'    => 'pop3',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'pop_ms_avg',
           }, ],
        },
     'connpool_latency'
     => {
         'vlabel'   => 'latency (ms)',
         'title'    => "Connection pool latency",
         'info'     => "Mailboxd connection pool get latencies according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'database',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'db_conn_ms_avg',
           },
           {
           'label'    => 'ldap',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'ldap_dc_ms_avg',
           }, ],
        },
     'jvm_heap_free'
     => {
         'vlabel'   => 'bytes',
         'title'    => "JVM heap free",
         'info'     => "Mailboxd JVM Heap Free according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'total',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'heap_free',
           },
           {
           'label'    => 'ps_old_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_old_gen_free',
           },
           {
           'label'    => 'cms_old_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_cms_old_gen_free',
           },
           {
           'label'    => 'tenured_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_tenured_gen_free',
           },
           {
           'label'    => 'train_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_train_gen_free',
           },
           {
           'label'    => 'ps_eden',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_eden_space_free',
           },
           {
           'label'    => 'ps_survivor',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_survivor_space_free',
           },
           {
           'label'    => 'par_eden',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_par_eden_space_free',
           },
           {
           'label'    => 'par_survivor',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_par_survivor_space_free',
           },
           {
           'label'    => 'eden',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_eden_space_free',
           },
           {
           'label'    => 'survivor',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_survivor_space_free',
           }, ],
        },
     'jvm_heap_used'
     => {
         'vlabel'   => 'bytes',
         'title'    => "JVM heap used",
         'info'     => "Mailboxd JVM Heap Used according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'total',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'heap_used',
           },
           {
           'label'    => 'ps_old_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_old_gen_used',
           },
           {
           'label'    => 'cms_old_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_cms_old_gen_used',
           },
           {
           'label'    => 'tenured_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_tenured_gen_used',
           },
           {
           'label'    => 'train_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_train_gen_used',
           },
           {
           'label'    => 'ps_eden',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_eden_space_used',
           },
           {
           'label'    => 'ps_survivor',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_survivor_space_used',
           },
           {
           'label'    => 'par_eden',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_par_eden_space_used',
           },
           {
           'label'    => 'par_survivor',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_par_survivor_space_used',
           },
           {
           'label'    => 'eden',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_eden_space_used',
           },
           {
           'label'    => 'survivor',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_survivor_space_used',
           }, ],
        },
     'jvm_major_gc_count'
     => {
         'vlabel'   => 'Count pr ${graph_period}',
         'title'    => "JVM major GC count",
         'info'     => "Mailboxd JVM Major GC Count according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'major',
           'type'     => 'DERIVE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'gc_major_count',
           }, ],
        },
     'jvm_major_gc_time'
     => {
         'vlabel'   => 'Time (ms) pr ${graph_period}',
         'title'    => "JVM major GC time",
         'info'     => "Mailboxd JVM Major GC Time according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'major',
           'type'     => 'DERIVE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'gc_major_ms',
           }, ],
        },
     'jvm_minor_gc_count'
     => {
         'vlabel'   => 'Count pr ${graph_period}',
         'title'    => "JVM minor GC count",
         'info'     => "Mailboxd JVM Minor GC Count according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'minor',
           'type'     => 'DERIVE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'gc_minor_count',
           }, ],
        },
     'jvm_minor_gc_time'
     => {
         'vlabel'   => 'Time (ms) pr ${graph_period}',
         'title'    => "JVM minor GC time",
         'info'     => "Mailboxd JVM Minor GC Time according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'minor',
           'type'     => 'DERIVE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'gc_minor_ms',
           }, ],
        },
     'jvm_permgen'
     => {
         'vlabel'   => 'bytes',
         'title'    => "JVM Perm Gen",
         'info'     => "Mailboxd JVM Permanent Generation and Code Cache according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'ps_perm_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_perm_gen_used',
           },
           {
           'label'    => 'perm_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_perm_gen_used',
           },
           {
           'label'    => 'cms_perm_gen',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_cms_perm_gen_used',
           },
           {
           'label'    => 'code_cache',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_code_cache_used',
           },
           {
           'label'    => 'perm_gen_shared_ro',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_perm_gen_[shared-ro]_used',
           },
           {
           'label'    => 'perm_gen_shared_rw',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_perm_gen_[shared-rw]_used',
           },
           {
           'label'    => 'ps_perm_gen_free',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_ps_perm_gen_free',
           },
           {
           'label'    => 'perm_gen_free',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_perm_gen_free',
           },
           {
           'label'    => 'cms_perm_gen_free',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_cms_perm_gen_free',
           },
           {
           'label'    => 'code_cache_free',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_code_cache_free',
           },
           {
           'label'    => 'perm_gen_shared_ro_free',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_perm_gen_[shared-ro]_free',
           },
           {
           'label'    => 'perm_gen_shared_rw_free',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mpool_perm_gen_[shared-rw]_free',
           }, ],
        },
     'lmtp_throughput'
     => {
         'vlabel'   => 'bytes/min',
         'title'    => "LMTP throughput",
         'info'     => "Mailboxd LMTP Throughput according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'received',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'lmtp_rcvd_bytes',
           },
           {
           'label'    => 'delivered',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'lmtp_dlvd_bytes',
           }, ],
        },
     'lmtp_delivery_rate'
     => {
         'vlabel'   => 'msgs/min',
         'title'    => "LMTP delivery rate",
         'info'     => "Mailboxd LMTP Delivery Rate according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'received',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'lmtp_rcvd_msgs',
           },
           {
           'label'    => 'delivered',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'lmtp_dlvd_msgs',
           },
           {
           'label'    => 'rcvd_rcpt',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'lmtp_rcvd_rcpt',
           'info'     => 'received messages * recipients',
           }, ],
        },
     'lucene_cachehit'
     => {
         'vlabel'   => 'hitrate %',
         'title'    => "Lucene cache hitrate",
         'info'     => "Mailboxd Lucene IndexWriterCache hitrate according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'total',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'idx_wrt_opened',
           'csvfield2'=> 'idx_wrt_opened_cache_hit',
           'graph'    => 'off'
           },
           {
           'label'    => 'hit',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'idx_wrt_opened_cache_hit',
           'draw'     => 'AREA',
           'cdef'     => 'hit,total,/,100,*'
           }, ],
        },
     'lucene_io'
     => {
         'vlabel'   => 'bytes',
         'title'    => "Lucene I/O",
         'info'     => "Mailboxd lucene I/O according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'write',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'idx_bytes_written',
           },
           {
           'label'    => 'read',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'idx_bytes_read',
           }, ],
        },
     'lucene_writers'
     => {
         'vlabel'   => 'writers',
         'title'    => "Lucene index writers",
         'info'     => "Mailboxd dirty lucene index writers according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'writers',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'idx_wrt_avg',
           }, ],
        },
     'mailbox_add_latency'
     => {
         'vlabel'   => 'latency (ms)',
         'title'    => "Mailbox add latency",
         'info'     => "Mailboxd Mailbox Add Latency (Delivery Speed) according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'latency',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_add_msg_ms_avg',
           }, ],
        },
     'mailbox_add_rate'
     => {
         'vlabel'   => 'msgs/min',
         'title'    => "Mailbox add rate",
         'info'     => "Mailboxd Mailbox Add Rate (Delivery Rate) according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'adds',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_add_msg_count',
           }, ],
        },
     'mailbox_cachehit'
     => {
         'vlabel'   => 'hitrate %',
         'title'    => "Mailbox cache hitrate",
         'info'     => "Mailboxd Mailbox Cache hitrate according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'hit',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_cache',
           }, ],
        },
     'mailbox_itemblob_cachehit'
     => {
         'vlabel'   => 'hitrate %',
         'title'    => "Mailbox item/blob cache",
         'info'     => "Mailboxd Mailbox Item/Blob Cache hitrate according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'blob',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_msg_cache',
           },
           {
           'label'    => 'metadata',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_item_cache',
           }, ],
        },
     # what does this actually measure? Hovers around 1 on an idle test server.
     'mailbox_get_count'
     => {
         'vlabel'   => 'count',
         'title'    => "Mailbox get count",
         'info'     => "Mailboxd Mailbox Get Count according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'get',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_get_count',
           }, ],
        },
     'mailbox_get_latency'
     => {
         'vlabel'   => 'latency (ms)',
         'title'    => "Mailbox get latency",
         'info'     => "Mailboxd Mailbox Get Latency according to zmstat/mailboxd.csv",
         'fields'   => [ {
           'label'    => 'latency',
           'type'     => 'GAUGE',
           'csv'      => 'mailboxd.csv',
           'csvfield' => 'mbox_get_ms_avg',
           }, ],
        },
     'postfix_queue'
     => {
         'vlabel'   => 'Messages queued',
         'title'    => "Postfix messages queued",
         'info'     => "Postfix messages queued according to zmstat/mtaqueue.csv",
         'fields'   => [ {
           'label'    => 'messages',
           'type'     => 'GAUGE',
           'csv'      => 'mtaqueue.csv',
           'csvfield' => 'requests',
           }, ],
         },
     'process_cpu'
     => {
         'vlabel'   => 'percent cpu',
         'title'    => "Process CPU usage",
         'info'     => "Process CPU usage according to zmstat/proc.csv",
         'fields'   => [ {
           'label'    => 'mailbox',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'mailbox-total-cpu',
           },
           {
           'label'    => 'mysql',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'mysql-total-cpu',
           },
           {
           'label'    => 'convertd',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'convertd-total-cpu',
           },
           {
           'label'    => 'ldap',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'ldap-total-cpu',
           },
           {
           'label'    => 'postfix',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'postfix-total-cpu',
           },
           {
           'label'    => 'amavis',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'amavis-total-cpu',
           },
           {
           'label'    => 'clam',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'clam-total-cpu',
           },
           {
           'label'    => 'stats',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'stats-total-cpu',
           }, ],
        },
     'process_mem_total'
     => {
         'vlabel'   => 'MB',
         'title'    => "Process Total Memory",
         'info'     => "Process Total memory usage according to zmstat/proc.csv",
         'fields'   => [ {
           'label'    => 'mailbox',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'mailbox-totalMB',
           },
           {
           'label'    => 'mysql',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'mysql-totalMB',
           },
           {
           'label'    => 'convertd',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'convertd-totalMB',
           },
           {
           'label'    => 'ldap',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'ldap-totalMB',
           },
           {
           'label'    => 'postfix',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'postfix-totalMB',
           },
           {
           'label'    => 'amavis',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'amavis-totalMB',
           },
           {
           'label'    => 'clam',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'clam-totalMB',
           },
           {
           'label'    => 'stats',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'stats-totalMB',
           }, ],
        },
     'process_mem_rss'
     => {
         'vlabel'   => 'MB',
         'title'    => "Process Resident Memory",
         'info'     => "Process resident memory (RSS) according to zmstat/proc.csv",
         'fields'   => [ {
           'label'    => 'mailbox',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'mailbox-rssMB',
           },
           {
           'label'    => 'mysql',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'mysql-rssMB',
           },
           {
           'label'    => 'convertd',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'convertd-rssMB',
           },
           {
           'label'    => 'ldap',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'ldap-rssMB',
           },
           {
           'label'    => 'postfix',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'postfix-rssMB',
           },
           {
           'label'    => 'amavis',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'amavis-rssMB',
           },
           {
           'label'    => 'clam',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'clam-rssMB',
           },
           {
           'label'    => 'stats',
           'type'     => 'GAUGE',
           'csv'      => 'proc.csv',
           'csvfield' => 'stats-rssMB',
           }, ],
        },
    );

# Config subroutine
sub config {
    my $action = shift;
    if (!(exists $ops{$action})) { die "wrong symlinked name, $action is not implemented\n"; }
    print <<EOF;
graph_args --base 1000 -l 0
graph_vlabel $ops{$action}->{'vlabel'}
graph_title $ops{$action}->{'title'}
graph_category Zimbra
graph_info $ops{$action}->{'info'}
EOF

    my $tmpfield = $ops{$action}->{'fields'};
    foreach my $field ( @$tmpfield )
    {
      my $label = $field->{'label'};
      print "$label.label $label\n";
      print "$label.type $field->{'type'}\n";
      print "$label.min 0\n";
      if (exists $field->{'graph'}) { print "$label.graph $field->{'graph'}\n"; }
      if (exists $field->{'info'}) { print "$label.info $field->{'info'}\n"; }
      if (exists $field->{'draw'}) { print "$label.draw $field->{'draw'}\n"; }
      if (exists $field->{'cdef'}) { print "$label.cdef $field->{'cdef'}\n"; }
    }
}

my $op;

if ($0 =~ /zimbra_([\w\d_]+)$/)
{
  $op = $1;
}

if ($ARGV[0]) {
    if ($ARGV[0] eq "config") {
        if ($op) {
            &config ($op);
        } else {
            die ("Can't run config without a symlinked name\n");
        }
    } elsif ($ARGV[0] eq "autoconf") {
        if ($ret ne "") {
          print "no ($ret)\n";
        }
        elsif (chdir $csvpath)
        {
          print "yes\n";
        }
        else
        {
          print "no (chdir to $csvpath failed)\n";
        }
    } elsif ($ARGV[0] eq "suggest") {
        foreach my $key ( keys %ops )
        {
          print "$key\n";
        }
    }
    exit 0;
}

# We won't run this without parameters.
die ("Can't run without a symlinked name\n") if $0 =~ /zimbra_$/;

my @cached_csvfields = ();

my $tmpfield = $ops{$op}->{'fields'};
foreach my $field ( @$tmpfield )
{
  my $label = $field->{'label'};
  my $csv = Text::CSV_XS->new ( { 'allow_whitespace' => 1 } );
  my $error;

  if (tie my @file, 'Tie::File', $csvpath . "/" . $field->{'csv'}, mode => O_RDONLY)
  {
    # parse the csv header line for the interesting field
    my $status = $csv->parse($file[0]);
    my @csvfields = $csv->fields();
    my $csvindex = 0; $csvindex++ until $csvindex > $#csvfields or $csvfields[$csvindex] eq $field->{'csvfield'};
    if ($csvindex > $#csvfields) { $error = 1; goto FIELDERR; }
    
    my $csvindex2;
    if (exists $field->{'csvfield2'})
    {
      $csvindex2 = 0; $csvindex2++ until $csvindex2 > $#csvfields or $csvfields[$csvindex2] eq $field->{'csvfield2'};
      if ($csvindex2 > $#csvfields) { $error = 1; goto FIELDERR; }
    }

    unless ($#cached_csvfields > -1)
    {
      # blindly use last line of the csv, for now
      $status = $csv->parse($file[-1]);

      @cached_csvfields = $csv->fields();
    }
    # empty csv field => undef
    if ($cached_csvfields[$csvindex] eq "") { $error = 1; goto FIELDERR; }

    if (defined $csvindex2)
    {
      print "$label.value " . ( $cached_csvfields[$csvindex] + $cached_csvfields[$csvindex2] ). "\n";
    } else {
      print "$label.value " . $cached_csvfields[$csvindex] . "\n";
    }
  } else { $error = 1; }

FIELDERR:
  if ($error) {
    print "$label.value UNDEF\n" ;
  }
}