@@ -35,6 +35,7 @@ import (
3535 "bufio"
3636 "fmt"
3737 "os"
38+ "regexp"
3839 "strings"
3940
4041 "github.com/ibm-messaging/mq-golang/ibmmq"
@@ -263,7 +264,7 @@ func discoverElements(ty *MonType) error {
263264 }
264265 }
265266
266- elem .MetricName = formatDescriptionElem (elem )
267+ elem .MetricName = formatDescription (elem )
267268 ty .Elements [elementIndex ] = elem
268269 }
269270 }
@@ -631,59 +632,49 @@ bytes etc), and organisation of the elements of the name (units last)
631632While we can't change the MQ-generated descriptions for its statistics,
632633we can reformat most of them heuristically here.
633634*/
634- func formatDescriptionElem (elem * MonElement ) string {
635- s := formatDescription (elem .Description )
636-
637- unit := ""
638- switch elem .Datatype {
639- case ibmmq .MQIAMO_MONITOR_MICROSEC :
640- // Although the qmgr captures in us, we convert when
641- // pushing out to the backend, so this label needs to match
642- unit = "_seconds"
643- }
644- s += unit
645-
646- return s
647- }
648-
649- func formatDescription (baseName string ) string {
650- s := baseName
635+ func formatDescription (elem * MonElement ) string {
636+ s := elem .Description
651637 s = strings .Replace (s , " " , "_" , - 1 )
652638 s = strings .Replace (s , "/" , "_" , - 1 )
653639 s = strings .Replace (s , "-" , "_" , - 1 )
654640
655- /* common pattern is "xxx - yyy" leading to 3 ugly adjacent underscores */
656- s = strings . Replace ( s , "___" , "_" , - 1 )
657- s = strings . Replace (s , "__" , "_" , - 1 )
641+ /* Make sure we don't have multiple underscores */
642+ multiunder := regexp . MustCompile ( "__*" )
643+ s = multiunder . ReplaceAllLiteralString (s , "_" )
658644
659645 /* make it all lowercase. Not essential, but looks better */
660646 s = strings .ToLower (s )
661647
662- // Do not use _count
648+ /* Remove all cases of bytes, seconds, count or percentage (we add them back in later) */
663649 s = strings .Replace (s , "_count" , "" , - 1 )
650+ s = strings .Replace (s , "_bytes" , "" , - 1 )
651+ s = strings .Replace (s , "_byte" , "" , - 1 )
652+ s = strings .Replace (s , "_seconds" , "" , - 1 )
653+ s = strings .Replace (s , "_second" , "" , - 1 )
654+ s = strings .Replace (s , "_percentage" , "" , - 1 )
664655
665656 // Switch round a couple of specific names
666- s = strings .Replace (s , "bytes_written" , "written_bytes" , - 1 )
667- s = strings .Replace (s , "bytes_max" , "max_bytes" , - 1 )
668- s = strings .Replace (s , "bytes_in_use" , "in_use_bytes" , - 1 )
669657 s = strings .Replace (s , "messages_expired" , "expired_messages" , - 1 )
670658
671- if strings .HasSuffix (s , "free_space" ) {
659+ // Add the unit at end
660+ switch elem .Datatype {
661+ case ibmmq .MQIAMO_MONITOR_PERCENT , ibmmq .MQIAMO_MONITOR_HUNDREDTHS :
672662 s = s + "_percentage"
673- s = strings .Replace (s , "__" , "_" , - 1 )
674- }
675-
676- // Make "byte", "file" and "message" units plural
677- if strings .HasSuffix (s , "byte" ) ||
678- strings .HasSuffix (s , "message" ) ||
679- strings .HasSuffix (s , "file" ) {
680- s = s + "s"
681- }
682-
683- // Move % to the end
684- if strings .Contains (s , "_percentage_" ) {
685- s = strings .Replace (s , "_percentage_" , "_" , - 1 )
686- s += "_percentage"
663+ case ibmmq .MQIAMO_MONITOR_MB , ibmmq .MQIAMO_MONITOR_GB :
664+ s = s + "_bytes"
665+ case ibmmq .MQIAMO_MONITOR_MICROSEC :
666+ s = s + "_seconds"
667+ default :
668+ if strings .Contains (s , "_total" ) {
669+ /* If we specify it is a total in description put that at the end */
670+ s = strings .Replace (s , "_total" , "" , - 1 )
671+ s = s + "_total"
672+ } else if strings .Contains (s , "log_" ) {
673+ /* Weird case where the log datatype is not MB or GB but should be bytes */
674+ s = s + "_bytes"
675+ } else {
676+ s = s + "_count"
677+ }
687678 }
688679
689680 return s
0 commit comments