Search code examples
c#.netbit-manipulationsyslog

Algorithm for parsing Syslog Priority Value (PRIVAL)


I'd like to parse the PRIVAL info from a syslog entry, but I'm having trouble wrapping my head around the algorithm needed.

RFC5424 says:

The Priority value is calculated by first multiplying the Facility number by 8 and then adding the numerical value of the Severity.

And with this, here is what I do understand.

(X * 8) + y = [known number]

so

If (X * 8) + Y = 134    
// I happen to know that X = 16 and Y = 6

Alternatively

If (X * 8) + Y = 78
// What are the values of X and Y?

So what would be an appropriate algorithm for parsing this information?


Solution

  • According to RFC 5424 the Priority Value is composed from a Facility value in the range 0..23 and a Severity value in the range 0..7. Given a Priority Value you can extract the Facility and Severity as follows:

    int priorityValue = 134; // using your example
    int facility = priorityValue >> 3;
    int severity = priorityValue & 7;
    

    This is a simple reversal of the composition operation priorityValue = facility * 8 + severity that is used to generate the values you are seeing in the SYSLOG data. You could also use:

    int facility = priorityValue / 8;
    

    Since we're working with integers this should give you the same results as the bit-shift operation above.