perlcacti

create output for cacti using perl


I have some trouble making the right script for cacti using perl.

This is the output of the data I can get:

Pac max phase 1: 2150W
Pac max phase 2: 0W 
Pac max phase 3: 0W
Energy Production:
        EToday: 7.282kWh
        ETotal: 1113.263kWh
        Operation Time: 2763.12h
        Feed-In Time  : 2376.42h
DC Spot Data:
        String 1 Pdc: 1917271.250kW - Udc: 280.00V - Idc:  2.118A
        String 2 Pdc: 1934451.875kW - Udc: 7789238.50V - Idc: 878461.750A
SUSyID: 246 - SN: 2002268779
AC Spot Data:
        Phase 1 Pac : 1635021.625kW - Uac: 237.10V - Iac: 1852400.000A
        Phase 2 Pac : 1730176.375kW - Uac:   0.00V - Iac: 1277195.375A
        Phase 3 Pac : 543451.500kW - Uac:   0.00V - Iac: 822742.312A
        Total Pac   :   0.560kW

I managed to get some data with a little script.

while(<STDIN>) {
chomp;
if (s/^Pac[ ]max[ ]phase[ ]1[]*[:][ ]*(\d+\.*\d+).*/\1/) {
print "Max:$_ ";
}
if (s/EToday[]*[:][ ]*(\d+\.*\d+).*/\1/) {
print "EToday:$_ ";
}
if (s/ETotal[]*[:][ ]*(\d+\.*\d+).*/\1/) {
print "ETotal:$_ ";
}

After EToday I get a lot off space in the return en I also want to get the value of Idc. I have troubles to make the right output. Can someone help me to create the following outputs:

Pac max phase 1: 
EToday: 
ETotal: 
Operation Time: 
Feed-In Time  : 
Udc:
Idc:
Uac:
Iac:
Total Pac   :
Grid Freq. : 

aaa:xxxx b:xxxx c:xxxx etc.

Kind regards, Martijn


Solution

  • You don't show Grid Freq. in your dample data, but does this program solve your problem?

    It forms a regex that matches any of the labels that you have listed, followed by a colon : and a number, printing out all the occurrences of a pattern like that in each line of the input.

    use strict;
    use warnings;
    
    my @fields = (
        'Pac max phase 1', 'Pac max phase 2', 'Pac max phase 3',
        'EToday', 'ETotal',
        'Operation Time', 'Feed-In Time',
        'Udc', 'Idc', 'Uac', 'Iac',
        'Total Pac',
        'Grid Freq.',
    );
    
    my $re = join '|', @fields;
    
    my %printed;
    
    while(<DATA>) {
      chomp;
      while ( /($re)\s*:\s*(\d+(?:\.\d+)?)/g )  {
        print "$1: $2\n" unless $printed{$1}++;
      }
    }
    
    __DATA__
    Pac max phase 1: 2150W
    Pac max phase 2: 0W 
    Pac max phase 3: 0W
    Energy Production:
            EToday: 7.282kWh
            ETotal: 1113.263kWh
            Operation Time: 2763.12h
            Feed-In Time  : 2376.42h
    DC Spot Data:
            String 1 Pdc: 1917271.250kW - Udc: 280.00V - Idc:  2.118A
            String 2 Pdc: 1934451.875kW - Udc: 7789238.50V - Idc: 878461.750A
    SUSyID: 246 - SN: 2002268779
    AC Spot Data:
            Phase 1 Pac : 1635021.625kW - Uac: 237.10V - Iac: 1852400.000A
            Phase 2 Pac : 1730176.375kW - Uac:   0.00V - Iac: 1277195.375A
            Phase 3 Pac : 543451.500kW - Uac:   0.00V - Iac: 822742.312A
            Total Pac   :   0.560kW
    

    output

    Pac max phase 1: 2150
    Pac max phase 2: 0
    Pac max phase 3: 0
    EToday: 7.282
    ETotal: 1113.263
    Operation Time: 2763.12
    Feed-In Time: 2376.42
    Udc: 280.00
    Idc: 2.118
    Uac: 237.10
    Iac: 1852400.000
    Total Pac: 0.560