Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
linux:grafana [2023/06/08 09:10] admin |
linux:grafana [2024/05/24 10:14] (текущий) admin |
||
---|---|---|---|
Строка 3: | Строка 3: | ||
===== Установка ===== | ===== Установка ===== | ||
<code bash> | <code bash> | ||
+ | # (не работает, | ||
# Добавляем ключи и репозиторий: | # Добавляем ключи и репозиторий: | ||
apt install gnupg2 && wget -q -O - https:// | apt install gnupg2 && wget -q -O - https:// | ||
Строка 47: | Строка 48: | ||
Список элементов в переменной и исключение по регулярке\\ | Список элементов в переменной и исключение по регулярке\\ | ||
{{: | {{: | ||
+ | |||
+ | Переменная, | ||
+ | {{: | ||
< | < | ||
Строка 64: | Строка 68: | ||
* count_non_null() — количество значений, | * count_non_null() — количество значений, | ||
</ | </ | ||
+ | |||
+ | ==== Общее ==== | ||
+ | **Единицы измерения в графиках**\\ | ||
+ | Для данных есть два варианта, | ||
+ | **EIC**, когда в килобайте 1024 байт еще называют " | ||
+ | |||
+ | |||
+ | ==== Repeat/ | ||
+ | Можно сделать автоматический репит графиков, | ||
+ | Например переменная со списком хостов, | ||
+ | " | ||
+ | {{: | ||
+ | |||
+ | |||
+ | |||
Строка 71: | Строка 90: | ||
Потребляет мало ресурсов благодаря неполной индексации. Интегрируется с разными системами в т.ч. с самой Grafana.\\ | Потребляет мало ресурсов благодаря неполной индексации. Интегрируется с разными системами в т.ч. с самой Grafana.\\ | ||
В **promtail** для выборки, | В **promtail** для выборки, | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Alerting ===== | ||
+ | |||
+ | < | ||
+ | < | ||
+ | <code go> | ||
+ | # Указание шаблона в настройках канала оповещения | ||
+ | {{ template " | ||
+ | |||
+ | |||
+ | # Колво алертов | ||
+ | {{ len .Alerts.Firing }} | ||
+ | {{ len .Alerts.Resolved }} | ||
+ | |||
+ | |||
+ | # Перечислены оповещения и их статус | ||
+ | {{ range .Alerts }} {{ .Status }}: {{ .Labels.alertname }} | ||
+ | {{ end }} | ||
+ | |||
+ | |||
+ | # Вложенные шаблоны: | ||
+ | # определение шаблона | ||
+ | {{ define " | ||
+ | ... | ||
+ | {{ end }} | ||
+ | |||
+ | |||
+ | # Использование | ||
+ | summary: | ||
+ | {{ template " | ||
+ | |||
+ | |||
+ | # Удаление тегов | ||
+ | {{ .Labels.Remove (stringSlice " | ||
+ | {{ .Labels.alertname}} | ||
+ | |||
+ | |||
+ | # Прием " | ||
+ | {{ with .ValueString }} | ||
+ | {{- . | reReplaceAll `\[\s` "" | ||
+ | {{ end }} | ||
+ | |||
+ | |||
+ | # Значения графика | ||
+ | {{ .Values.max_mem }} | ||
+ | {{ .ValueString }} | ||
+ | {{- printf "%s - %s" .Labels.instance .Annotations.summary }} | ||
+ | {{ printf " | ||
+ | {{ index $values printf " | ||
+ | |||
+ | |||
+ | # Так же, хороший совет занести нужные данные в summary/ | ||
+ | # затем в шаблоне обращаться к этому полю для получения данных | ||
+ | # Поле Description в алерте: | ||
+ | " | ||
+ | # В шаблоне: | ||
+ | {{ index .Annotations " | ||
+ | |||
+ | |||
+ | # Переменная (?) | ||
+ | {{- $name := default .Chart.Name .Values.nameOverride }} | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | < | ||
+ | |||
+ | <code go> | ||
+ | {{ define " | ||
+ | {{range .Alerts}} | ||
+ | {{ .Values.used_percent }} | ||
+ | {{ .Labels.alertname}} | ||
+ | {{ index .Annotations " | ||
+ | {{- end }} | ||
+ | {{ end }} | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code go> | ||
+ | {{ define " | ||
+ | Summary: {{ .CommonAnnotations.Values }} | ||
+ | | ||
+ | {{ len .Alerts.Firing }} firing alert(s): | ||
+ | {{ if .Alerts.Firing -}} | ||
+ | {{ range .Alerts.Firing -}} | ||
+ | {{ .Labels.Remove (stringSlice " | ||
+ | {{ with .ValueString }} | ||
+ | {{- . | reReplaceAll `\[\s` "" | ||
+ | {{ end }} | ||
+ | {{- end }} | ||
+ | {{- end }} | ||
+ | | ||
+ | {{ len .Alerts.Resolved }} resolved alert(s): | ||
+ | {{ if .Alerts.Resolved -}} | ||
+ | {{ range .Alerts.Resolved -}} | ||
+ | {{ .Labels.Remove (stringSlice " | ||
+ | {{ with .ValueString }} | ||
+ | {{- . | reReplaceAll `\[\s` "" | ||
+ | {{ end }} | ||
+ | {{- end }} | ||
+ | {{- end }} | ||
+ | {{ end }} | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code go> | ||
+ | {{ define " | ||
+ | {{ if .Alerts.Firing }} | ||
+ | **{{ len .Alerts.Firing }} firing alert(s)** | ||
+ | {{ template " | ||
+ | {{- end }} | ||
+ | |||
+ | {{- if .Alerts.Resolved -}} | ||
+ | **{{ len .Alerts.Resolved }} resolved alert(s)** | ||
+ | {{ template " | ||
+ | {{- end }} | ||
+ | {{- end }} | ||
+ | |||
+ | {{ define " | ||
+ | {{ range . }} | ||
+ | {{- printf "%s - %s" .Labels.instance .Annotations.summary }} | ||
+ | {{ end }} | ||
+ | {{- end }} | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code go> | ||
+ | {{ define " | ||
+ | {{- if eq ((index .Alerts 0).Labels._alert_type_) " | ||
+ | {{- template " | ||
+ | {{- if eq ((index .Alerts 0).Labels._alert_type_) " | ||
+ | {{- template " | ||
+ | |||
+ | {{/ | ||
+ | {{ define " | ||
+ | {{if .Alerts.Firing }} [Alerting] {{end}}{{ if .Alerts.Resolved }} [OK] {{end}}{{ .CommonLabels.alertname }} | ||
+ | {{ end }} | ||
+ | |||
+ | {{/ | ||
+ | {{ define " | ||
+ | {{ if and (gt (len .Alerts.Firing) 0) (eq (len .Alerts.Resolved) 0) -}} | ||
+ | [FIRING:{{ len .Alerts.Firing }}] {{ .CommonLabels.alertname }} | ||
+ | {{ else }}{{ if and (gt (len .Alerts.Resolved) 0) (eq (len .Alerts.Firing) 0) -}} | ||
+ | [RESOLVED] {{ .CommonLabels.alertname }} | ||
+ | {{ else -}} | ||
+ | [FIRING:{{ len .Alerts.Firing }}, | ||
+ | {{ end }}{{ end }}{{ end }} | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | < | ||
+ | Чтобы получить значение, | ||
+ | <code go> | ||
+ | // после " | ||
+ | <b>{{ $labels.group }}</ | ||
+ | </ | ||
+ | |||
+ | Пример шаблона | ||
+ | <code go> | ||
+ | {{ define " | ||
+ | {{/* Стремление сделать один шаблон, | ||
+ | с одним инстансом или с мульти, | ||
+ | {{/* Примечательно, | ||
+ | некоторые строки здесь выпадают из форматирования и прибиты к левой границе */}} | ||
+ | {{- if eq ((index .Alerts 0).Labels._alert_type_) " | ||
+ | {{- template " | ||
+ | {{ else if eq ((index .Alerts 0).Labels._alert_type_) " | ||
+ | {{- template " | ||
+ | {{ else }} | ||
+ | {{- (index .Alerts 0).Labels.alertname }} | ||
+ | Label ' | ||
+ | {{ end }} | ||
+ | {{- end }} | ||
+ | |||
+ | {{/* Тип алертов с одним инстансом (CLASSIC) */}} | ||
+ | {{ define " | ||
+ | {{/* Возникновение алерта */}} | ||
+ | {{- if .Alerts.Firing -}} | ||
+ | {{ range .Alerts }}[Alerting] {{ .Labels.alertname }} | ||
+ | {{ if .Annotations.description }} | ||
+ | {{ .Annotations.description }} | ||
+ | {{ end }} | ||
+ | |||
+ | Metrics: | ||
+ | {{- $value_of_metric := (.ValueString | reReplaceAll `.*metric=' | ||
+ | {{ if ne $value_of_metric " | ||
+ | {{ with .ValueString }} | ||
+ | {{- . | reReplaceAll `\[\s` "" | ||
+ | {{ end }} | ||
+ | {{ else }} | ||
+ | {{ with .ValueString }} | ||
+ | {{- . | reReplaceAll `\[\s` "" | ||
+ | {{- end }} | ||
+ | {{- end -}} | ||
+ | |||
+ | {{- if .PanelURL }} | ||
+ | Panel URL: {{ .PanelURL }} | ||
+ | {{ end }} | ||
+ | {{ end }} | ||
+ | {{ end -}} | ||
+ | |||
+ | {{/* Решение алерта */}} | ||
+ | {{- if .Alerts.Resolved -}} | ||
+ | {{ range .Alerts }}[OK] {{ .Labels.alertname }} | ||
+ | {{ .Annotations.description }} | ||
+ | {{- end }} | ||
+ | {{- end -}} | ||
+ | |||
+ | {{ end }} | ||
+ | |||
+ | |||
+ | {{/* Тип алерта, | ||
+ | {{ define " | ||
+ | {{ if (index .Alerts 0).Annotations.description -}} | ||
+ | {{ (index .Alerts 0).Annotations.description -}} | ||
+ | {{ end -}} | ||
+ | |||
+ | {{/* Если состояние Firing, перечисляем переданные инстансы в подшаблоне " | ||
+ | {{- if .Alerts.Firing }} | ||
+ | **{{ len .Alerts.Firing }} firing alert(s)** | ||
+ | {{ template " | ||
+ | {{ end -}} | ||
+ | |||
+ | {{/* Аналогично для состояния Resolve */}} | ||
+ | {{- if .Alerts.Resolved }} | ||
+ | **{{ len .Alerts.Resolved }} resolved alert(s)** | ||
+ | {{- template " | ||
+ | {{ end -}} | ||
+ | |||
+ | {{ if (index .Alerts 0).PanelURL }} | ||
+ | Panel URL: {{ (index .Alerts 0).PanelURL }} | ||
+ | {{- end }} | ||
+ | {{- end }} | ||
+ | |||
+ | {{/* Подшаблон для перечисления инстансов в цикле */}} | ||
+ | {{ define " | ||
+ | {{- range . -}} | ||
+ | {{/* Пишем поле " | ||
+ | в алерте там переменные с данными */}} | ||
+ | {{ index .Annotations " | ||
+ | {{ end -}} | ||
+ | {{ end }} | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Еще про алерты ===== | ||
+ | |||
+ | |||
+ | < | ||
+ | < | ||
+ | |||
+ | Собсна в простом случае, | ||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | <code bash> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | < | ||
+ | <code bash> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | < | ||
+ | <code bash> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== API ===== | ||
+ | |||
+ | < | ||
+ | < | ||
+ | |||
+ | <code bash> | ||
+ | curl http:// | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | < | ||
+ | < | ||
+ | |||
+ | Получить перечень | ||
+ | <code bash> | ||
+ | curl -X ' | ||
+ | -H ' | ||
+ | |||
+ | http:// | ||
+ | </ | ||
+ | |||
+ | |||
+ | Создать новый | ||
+ | <code bash> | ||
+ | curl -X ' | ||
+ | ' | ||
+ | -H ' | ||
+ | -H ' | ||
+ | -d '{ | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | }' | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||