Skip to content

Commit b77b8d6

Browse files
committed
improve handing of negative fields
1 parent 127cfbc commit b77b8d6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode.Protocol/MuninProtocolHandler.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,17 @@ private static void WriteConfigDataSourceAttributes(
597597
List<string> responseLines
598598
)
599599
{
600+
var shouldHandleNegativeFields = dataSource
601+
.Fields
602+
.Any(static f => !string.IsNullOrEmpty(f.Attributes.NegativeFieldName));
603+
600604
// The fields referenced by {fieldname}.negative must be defined ahread of others,
601605
// and thus lists the negative field settings first.
602606
// Otherwise, the following error occurs when generating the graph.
603607
// "[RRD ERROR] Unable to graph /var/cache/munin/www/XXX.png : undefined v name XXXXXXXXXXXXXX"
604-
var orderedFields = dataSource.Fields.OrderBy(f => IsNegativeField(f, dataSource.Fields) ? 0 : 1);
608+
IEnumerable<IPluginField> orderedFields = shouldHandleNegativeFields
609+
? dataSource.Fields.OrderBy(f => IsNegativeField(f, dataSource.Fields) ? 0 : 1)
610+
: dataSource.Fields;
605611

606612
foreach (var field in orderedFields) {
607613
var fieldAttrs = field.Attributes;
@@ -618,7 +624,7 @@ List<string> responseLines
618624
if (FormatNormalValueRange(fieldAttrs.NormalRangeForCritical) is string attrCritical)
619625
responseLines.Add($"{field.Name}.critical {attrCritical}");
620626

621-
if (!string.IsNullOrEmpty(fieldAttrs.NegativeFieldName)) {
627+
if (shouldHandleNegativeFields && !string.IsNullOrEmpty(fieldAttrs.NegativeFieldName)) {
622628
var negativeField = dataSource.Fields.FirstOrDefault(
623629
f => string.Equals(fieldAttrs.NegativeFieldName, f.Name, StringComparison.Ordinal)
624630
);
@@ -628,7 +634,7 @@ List<string> responseLines
628634
}
629635

630636
// this field is defined as the negative field of other field, so should not be graphed
631-
if (IsNegativeField(field, dataSource.Fields))
637+
if (shouldHandleNegativeFields && IsNegativeField(field, dataSource.Fields))
632638
graph = false;
633639

634640
if (graph is bool drawGraph)

0 commit comments

Comments
 (0)