[dvblast-devel] patches for dvblast - 3 mrtg monitor file

Christophe Massiot cmassiot at openheadend.tv
Tue Aug 23 00:27:51 CEST 2011


Hi,

BTW, could you provide a little more details on how to integrate that with MRTG? I am not very familiar with it but couldn't find any obvious way.

Also, is there a possibility to do the same with tools like RRD/collectd, which seem to be a little more trendy these days?

Le 21 juil. 2011 à 18:24, Peter Martin a écrit :

> This patch adds the feature where dvblast will write to an external file data that can be used by mrtg to display graphs of input bytes and any errors. The errors are split into sequence errors and TS packets with the error bit set, and finally any packets with the scrambling bit set.
> 
> We have used -Z for the command line arg, but I see that has been used by a recent change.
> 
> 
> 
> diff -Naur dvblast-svn-31-3-2011.orig/dvblast.c dvblast-svn-31-3-2011/dvblast.c
> --- dvblast-svn-31-3-2011.orig/dvblast.c    2011-03-31 13:12:32.000000000 +0100
> +++ dvblast-svn-31-3-2011/dvblast.c    2011-05-31 16:37:48.000000000 +0100
> @@ -48,6 +48,8 @@
>  #include <bitstream/dvb/si.h>
>  #include <bitstream/ietf/rtp.h>
>  
> +#include "mrtg-cnt.h"
> +
>  /*****************************************************************************
>   * Local declarations
>   *****************************************************************************/
> @@ -102,6 +104,9 @@
>  static mtime_t i_retention_global = DEFAULT_MAX_RETENTION;
>  static int i_ttl_global = 64;
>  
> +/* TPS Input log filename */
> +char * psz_mrtg_file = NULL;
> +
>  void (*pf_Open)( void ) = NULL;
>  block_t * (*pf_Read)( mtime_t i_poll_timeout ) = NULL;
>  void (*pf_Reset)( void ) = NULL;
> @@ -438,6 +443,7 @@
>      msg_Raw( NULL, "  -Q --quit-timeout     when locked, quit after this delay (in ms), or after the first lock timeout" );
>      msg_Raw( NULL, "  -r --remote-socket <remote socket>" );
>      msg_Raw( NULL, "  -V --version          only display the version" );
> +    msg_Raw( NULL, "  -Z mrtg-file <file>   Log input packets and errors into mrtg-file" );
>      exit(1);
>  }
>  
> @@ -508,7 +514,7 @@
>          { 0, 0, 0, 0 }
>      };
>  
> -    while ( (c = getopt_long(i_argc, pp_argv, "q::c:r:t:o:i:a:n:f:F:R:s:S:v:pb:I:m:P:K:G:H:X:O:uwUTL:E:d:D:A:lCeM:N:j:J:x:Q:hV", long_options, NULL)) != -1 )
> +    while ( (c = getopt_long(i_argc, pp_argv, "q::c:r:t:o:i:a:n:f:F:R:s:S:v:pb:I:m:P:K:G:H:X:O:uwUTL:E:d:D:A:lCeM:N:j:J:x:Q:hViZ:", long_options, NULL)) != -1 )
>      {
>          switch ( c )
>          {
> @@ -751,6 +757,10 @@
>              exit(0);
>              break;
>  
> +        case 'Z':
> +            psz_mrtg_file = optarg;
> +            break;
> +
>          case 'h':
>          default:
>              usage();
> @@ -855,6 +865,9 @@
>      srand( time(NULL) * getpid() );
>  
>      demux_Open();
> + 
> +    // init the mrtg logfile
> +    mrtgInit(psz_mrtg_file);
>  
>      if ( i_priority > 0 )
>      {
> @@ -888,13 +901,16 @@
>              exit(EXIT_SUCCESS);
>  
>          p_ts = pf_Read( i_poll_timeout );
> -        if ( p_ts != NULL )
> +        if ( p_ts != NULL ) {
> +        mrtgAnalyse(p_ts);
>              demux_Run( p_ts );
> +        }
>          i_poll_timeout = output_Send();
>          if ( i_poll_timeout == -1 || i_poll_timeout > MAX_POLL_TIMEOUT )
>              i_poll_timeout = MAX_POLL_TIMEOUT;
>      }
>  
> +    mrtgClose();
>      if ( b_enable_syslog )
>          msg_Disconnect();
>      return EXIT_SUCCESS;
> diff -Naur dvblast-svn-31-3-2011.orig/Makefile dvblast-svn-31-3-2011/Makefile
> --- dvblast-svn-31-3-2011.orig/Makefile    2011-03-31 13:12:32.000000000 +0100
> +++ dvblast-svn-31-3-2011/Makefile    2011-05-31 16:37:48.000000000 +0100
> @@ -10,7 +10,7 @@
>  LDLIBS += -lrt
>  LDLIBS_DVBLAST += -lpthread
>  
> -OBJ_DVBLAST = dvblast.o util.o dvb.o udp.o asi.o demux.o output.o en50221.o comm.o
> +OBJ_DVBLAST = dvblast.o util.o dvb.o udp.o asi.o demux.o output.o en50221.o comm.o mrtg-cnt.o
>  OBJ_DVBLASTCTL = util.o dvblastctl.o
>  
>  PREFIX ?= /usr/local
> diff -Naur dvblast-svn-31-3-2011.orig/mrtg-cnt.c dvblast-svn-31-3-2011/mrtg-cnt.c
> --- dvblast-svn-31-3-2011.orig/mrtg-cnt.c    1970-01-01 01:00:00.000000000 +0100
> +++ dvblast-svn-31-3-2011/mrtg-cnt.c    2011-05-31 16:37:48.000000000 +0100
> @@ -0,0 +1,176 @@
> +// vim: set shiftwidth=4 tabstop=4 expandtab autoindent :
> +//////////////////////////////////////////////////////////////////
> +// Handle dvb TS packets and count them for MRTG
> +///////////////////////////////////////////////////
> +// Copyright Tripleplay service 2004,2005
> +///////////////////////////////////////////////////
> +
> +#include <stdint.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <stdio.h>
> +#include <stdbool.h>
> +#include <sys/types.h>
> +
> +#include <unistd.h>
> +#include <fcntl.h>
> +#include <sys/time.h>
> +
> +#include "dvblast.h"
> +
> +// File handle
> +static FILE *mrtg_fh = NULL;
> +
> +// Counts
> +static long long l_mrtg_packets = 0;    // Packets received
> +static long long l_mrtg_seq_err_packets = 0;    // Out of sequence packets received
> +static long long l_mrtg_error_packets = 0;      // Packets received with the error flag set
> +static long long l_mrtg_scram_packets = 0;      // Scrambled Packets received
> +
> +// Reporting timer
> +#if defined( WIN32 )
> +static LARGE_INTEGER mrtg_time;
> +static LARGE_INTEGER mrtg_inc;
> +#else
> +static struct timeval mrtg_time = { 0, 0 };
> +#endif
> +
> +// Define the dump period in seconds
> +#define MRTG_INTERVAL   10
> +
> +// Pid sequence numbers
> +#define PIDS    0x2000
> +static signed char i_pid_seq[PIDS];
> +
> +// Report the mrtg counters: bytes received, error packets & sequence errors
> +static void dumpCounts()
> +{
> +    unsigned int multiplier = 1;        //MRTG_INTERVAL;
> +    if(mrtg_fh) {
> +        rewind(mrtg_fh);
> +        fprintf(mrtg_fh, "%lld %lld %lld %lld\n",
> +                l_mrtg_packets * 188 * multiplier,
> +                l_mrtg_error_packets * multiplier,
> +                l_mrtg_seq_err_packets * multiplier,
> +                l_mrtg_scram_packets * multiplier);
> +        fflush(mrtg_fh);
> +    }
> +}
> +
> +// analyse the input block counting packets and errors
> +// The input is a pointer to a block_t structure, which might be a linked list
> +// of blocks. Each block has one TS packet.
> +int mrtgAnalyse(block_t * p_ts)
> +{
> +    unsigned int i_pid;
> +    block_t *p_block = p_ts;
> +    
> +    while (p_block != NULL) {
> +        uint8_t *ts_packet = p_block->p_ts;
> +
> +        char i_seq, i_last_seq;
> +        l_mrtg_packets++;
> +
> +        if (ts_packet[0] != 0x47) {
> +            l_mrtg_error_packets++;
> +            p_block = p_block->p_next;
> +            continue;
> +        }
> +
> +        if (ts_packet[1] & 0x80) {
> +            l_mrtg_error_packets++;
> +            p_block = p_block->p_next;
> +            continue;
> +        }
> +
> +        i_pid = (ts_packet[1] & 0x1f) << 8 | ts_packet[2];
> +
> +        // Just count null packets - don't check the sequence numbering
> +        if (i_pid == 0x1fff) {
> +            p_block = p_block->p_next;
> +            continue;
> +        }
> +
> +        if (ts_packet[3] & 0xc0) {
> +            l_mrtg_scram_packets++;
> +        }
> +        // Check the sequence numbering
> +        i_seq = ts_packet[3] & 0xf;
> +        i_last_seq = i_pid_seq[i_pid];
> +
> +        if (i_last_seq == -1) {
> +            // First packet - ignore the sequence
> +        } else if (ts_packet[3] & 0x10) {
> +            // Packet contains payload - sequence should be up by one
> +            if (i_seq != ((i_last_seq + 1) & 0x0f)) {
> +                l_mrtg_seq_err_packets++;
> +            }
> +        } else {
> +            // Packet contains no payload - sequence should be unchanged
> +            if (i_seq != i_last_seq) {
> +                l_mrtg_seq_err_packets++;
> +            }
> +        }
> +        i_pid_seq[i_pid] = i_seq;
> +
> +        // Look at next block
> +        p_block = p_block->p_next;
> +    }
> +
> +    // All blocks processed. See if we need to dump the stats
> +    struct timeval now;
> +    gettimeofday(&now, NULL);
> +    if (timercmp(&now, &mrtg_time, >)) {
> +        // Time to report the mrtg counters
> +        dumpCounts();
> +
> +        // Set the timer for next time
> +        //
> +        // Normally we add the interval to the previous time so that if one
> +        // dump is a bit late, the next one still occurs at the correct time.
> +        // However, if there is a long gap (e.g. because the channel has
> +        // stopped for some time), then just rebase the timing to the current
> +        // time.  I've chosen MRTG_INTERVAL as the long gap - this is arbitary
> +        if ((now.tv_sec - mrtg_time.tv_sec) > MRTG_INTERVAL) {
> +            msg_Dbg(NULL, "Dump is %d seconds late - reset timing\n",
> +                    (int) (now.tv_sec - mrtg_time.tv_sec));
> +            mrtg_time = now;
> +        }
> +        mrtg_time.tv_sec += MRTG_INTERVAL;
> +    }
> +
> +    return 0;
> +}
> +
> +int mrtgInit(char *mrtg_file)
> +{
> +
> +    /* Open MRTG file */
> +    msg_Dbg(NULL, "Opening mrtg file %s.\n", mrtg_file);
> +    if ((mrtg_fh = fopen(mrtg_file, "wb")) == NULL) {
> +        msg_Err(NULL, "unable to open mrtg file");
> +        return -1;
> +    }
> +    // Initialise the file
> +    fprintf(mrtg_fh, "0 0 0 0\n");
> +    fflush(mrtg_fh);
> +
> +    // Initialise the sequence numbering
> +    memset(&i_pid_seq[0], -1, sizeof(signed char) * PIDS);
> +
> +    // Set the reporting timer
> +    gettimeofday(&mrtg_time, NULL);
> +    mrtg_time.tv_sec += MRTG_INTERVAL;
> +
> +    return 0;
> +}
> +
> +void mrtgClose()
> +{
> +    // This is only for testing when using filetest.
> +    if (mrtg_fh) {
> +        dumpCounts();
> +        fclose(mrtg_fh);
> +        mrtg_fh = NULL;
> +    }
> +}
> diff -Naur dvblast-svn-31-3-2011.orig/mrtg-cnt.h dvblast-svn-31-3-2011/mrtg-cnt.h
> --- dvblast-svn-31-3-2011.orig/mrtg-cnt.h    1970-01-01 01:00:00.000000000 +0100
> +++ dvblast-svn-31-3-2011/mrtg-cnt.h    2011-05-31 16:37:48.000000000 +0100
> @@ -0,0 +1,13 @@
> +// vim: set shiftwidth=4 tabstop=4 expandtab autoindent :
> +///////////////////////////////////////////////////
> +// Copyright Tripleplay service 2004,2005
> +///////////////////////////////////////////////////
> +
> +#ifndef MRTG_CNT_H
> +#define MRTG_CNT_H
> +
> +int mrtgInit(char *mrtg_file);
> +void mrtgClose();
> +int mrtgAnalyse(block_t * p_ts);
> +
> +#endif
> 
> -- 
> 
> Best Regards,
> 
> Dr. Peter Martin
> Chief Technical Officer
> 
> Tripleplay Services Ltd
> 
> Tel: +44 (0) 845 643 2057
> Support: +44 (0) 845 094 3357
> Mobile: +44 (0) 7810 876713
> Mail: peter.martin at tripleplay-services.com
> WWW: http://www.tripleplay-services.com
> 
> Tripleplay Services Ltd.
> Unit 3, Concept Park, Yarrow Road, POOLE, DORSET, BH12 4QT
> 
> This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited 
> 
> Tripleplay Services Limited is a private limited liability company in England and Wales, whose registered office is Cromwell House, 14 Fulwood Place, London, WC1V 6HZ. Registered in England, number 4338092.
> _______________________________________________
> dvblast-devel mailing list
> dvblast-devel at videolan.org
> http://mailman.videolan.org/listinfo/dvblast-devel



More information about the dvblast-devel mailing list