|
| 1 | +// Licensed to Apache Software Foundation (ASF) under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Apache Software Foundation (ASF) licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// Package alarm exposes the admin-server alarm runtime status (loaded rule |
| 19 | +// definitions and per-entity evaluation/window state). This is distinct from the |
| 20 | +// top-level `swctl alarm` command, which reads fired alarm records via GraphQL. |
| 21 | +package alarm |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + |
| 26 | + "github.com/urfave/cli/v2" |
| 27 | + |
| 28 | + "github.com/apache/skywalking-cli/pkg/admin/preflight" |
| 29 | + "github.com/apache/skywalking-cli/pkg/admin/status" |
| 30 | + "github.com/apache/skywalking-cli/pkg/display" |
| 31 | + "github.com/apache/skywalking-cli/pkg/display/displayable" |
| 32 | +) |
| 33 | + |
| 34 | +var Command = &cli.Command{ |
| 35 | + Name: "alarm", |
| 36 | + Usage: "Inspect alarm runtime status from the admin-server `status` module", |
| 37 | + UsageText: `Inspect the alarm-running kernel: loaded rule definitions and per-entity |
| 38 | +evaluation/window state. This differs from "swctl alarm list", which returns fired |
| 39 | +alarm records from the GraphQL surface.`, |
| 40 | + Subcommands: []*cli.Command{ |
| 41 | + rulesCommand, |
| 42 | + ruleCommand, |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +var rulesCommand = &cli.Command{ |
| 47 | + Name: "rules", |
| 48 | + Usage: "List the loaded alarm rules per OAP node (GET /status/alarm/rules)", |
| 49 | + UsageText: `List the loaded alarm rules, fanned out across every OAP node. |
| 50 | +
|
| 51 | +Examples: |
| 52 | +1. List alarm rules: |
| 53 | +$ swctl admin alarm rules`, |
| 54 | + Action: func(ctx *cli.Context) error { |
| 55 | + rules, err := status.AlarmRules(ctx.Context) |
| 56 | + if err != nil { |
| 57 | + return preflight.Explain(ctx.Context, err, preflight.ModuleStatus, "SW_STATUS") |
| 58 | + } |
| 59 | + return display.Display(ctx.Context, &displayable.Displayable{Data: rules}) |
| 60 | + }, |
| 61 | +} |
| 62 | + |
| 63 | +var ruleCommand = &cli.Command{ |
| 64 | + Name: "rule", |
| 65 | + Usage: "Show one alarm rule's definition and running state (GET /status/alarm/{ruleId}[/{entityName}])", |
| 66 | + ArgsUsage: "<ruleId> [<entityName>]", |
| 67 | + UsageText: `Show the definition and running state of a single alarm rule. When an |
| 68 | +entity name is given, the per-entity evaluation/window state is returned instead. |
| 69 | +
|
| 70 | +Examples: |
| 71 | +1. Show a rule's running state: |
| 72 | +$ swctl admin alarm rule service_resp_time_rule |
| 73 | +
|
| 74 | +2. Show the per-entity state of a rule: |
| 75 | +$ swctl admin alarm rule service_resp_time_rule mock_b_service`, |
| 76 | + Action: func(ctx *cli.Context) error { |
| 77 | + args := ctx.Args() |
| 78 | + ruleID := args.Get(0) |
| 79 | + if ruleID == "" { |
| 80 | + return fmt.Errorf("a <ruleId> argument is required") |
| 81 | + } |
| 82 | + entityName := args.Get(1) |
| 83 | + |
| 84 | + var ( |
| 85 | + result *status.ClusterAlarmStatus |
| 86 | + err error |
| 87 | + ) |
| 88 | + if entityName == "" { |
| 89 | + result, err = status.AlarmRule(ctx.Context, ruleID) |
| 90 | + } else { |
| 91 | + result, err = status.AlarmRuleEntity(ctx.Context, ruleID, entityName) |
| 92 | + } |
| 93 | + if err != nil { |
| 94 | + return preflight.Explain(ctx.Context, err, preflight.ModuleStatus, "SW_STATUS") |
| 95 | + } |
| 96 | + return display.Display(ctx.Context, &displayable.Displayable{Data: result}) |
| 97 | + }, |
| 98 | +} |
0 commit comments