HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/grafana.cpython-310.pyc
o

�N�g0�@sfdZddlZddlZddlmZddlmZdd�Zdd�Z	d	d
�Z
					ddd
�Zddd�ZdS)aC
Manage Grafana Dashboards

This module uses ``elasticsearch``, which can be installed via package, or pip.

You can specify elasticsearch hosts directly to the module, or you can use an
elasticsearch profile via pillars:

.. code-block:: yaml

    mygrafanaprofile:
      hosts:
        - es1.example.com:9200
        - es2.example.com:9200
      index: grafana-dash

.. code-block:: yaml

    # Basic usage (uses default pillar profile key 'grafana')
    Ensure myservice dashboard is managed:
      grafana.dashboard_present:
        - name: myservice
        - dashboard_from_pillar: default
        - rows_from_pillar:
            - systemhealth
            - requests

    # Passing hosts in
    Ensure myservice dashboard is managed:
      grafana.dashboard_present:
        - name: myservice
        - dashboard_from_pillar: default
        - rows:
            - collapse: false
              editable: true
              height: 150px
              title: System Health
              panels:
                - aliasColors: {}
                  id: 200000
                  annotate:
                    enable: false
                  bars: false
                  datasource: null
                  editable: true
                  error: false
                  fill: 7
                  grid:
                    leftMax: 100
                    leftMin: null
                    rightMax: null
                    rightMin: null
                    threshold1: 60
                    threshold1Color: rgb(216, 27, 27)
                    threshold2: null
                    threshold2Color: rgba(234, 112, 112, 0.22)
                  leftYAxisLabel: ''
                  legend:
                    avg: false
                    current: false
                    max: false
                    min: false
                    show: false
                    total: false
                    values: false
                  lines: true
                  linewidth: 1
                  nullPointMode: connected
                  percentage: false
                  pointradius: 5
                  points: false
                  renderer: flot
                  resolution: 100
                  scale: 1
                  seriesOverrides: []
                  span: 4
                  stack: false
                  steppedLine: false
                  targets:
                    - target: cloudwatch.aws.ec2.mysrv.cpuutilization.average
                  title: CPU (asg average)
                  tooltip:
                    query_as_alias: true
                    shared: false
                    value_type: cumulative
                  type: graph
                  x-axis: true
                  y-axis: true
                  y_formats:
                    - short
                    - short
                  zerofill: true
        - rows_from_pillar:
          - systemhealth
          - requests
        - profile:
            hosts:
              - es1.example.com:9200
              - es2.example.com:9200
            index: grafana-dash

    # Using a profile from pillars
    Ensure myservice dashboard is managed:
      grafana.dashboard_present:
        - name: myservice
        - dashboard:
            annotations:
              enable: true
              list: []
            editable: true
            hideAllLegends: false
            hideControls: false
            nav:
              - collapse: false
                enable: true
                notice: false
                now: true
                refresh_intervals:
                  - 10s
                  - 30s
                  - 1m
                  - 5m
                  - 15m
                  - 30m
                  - 1h
                  - 2h
                  - 1d
                status: Stable
                time_options:
                  - 5m
                  - 15m
                  - 1h
                  - 2h
                  - 3h
                  - 4h
                  - 6h
                  - 12h
                  - 1d
                  - 2d
                  - 4d
                  - 7d
                  - 16d
                  - 30d
                type: timepicker
            originalTitle: dockerregistry
            refresh: 1m
            rows: []
            sharedCrosshair: false
            style: dark
            tags: []
            templating:
              enable: true
              list: []
            time:
              from: now-2h
              to: now
            timezone: browser
        - rows_from_pillars:
          - systemhealth
          - requests
        - profile: mygrafanaprofile

The behavior of this module is to create dashboards if they do not exist, to
add rows if they do not exist in existing dashboards, and to update rows if
they exist in dashboards. The module will not manage rows that are not defined,
allowing users to manage their own custom rows.
�N)�SaltInvocationError)�
DictDiffercCsdtvrdSdS)z,
    Only load if grafana is available.
    �elasticsearch.exists�grafana)Fz(elasticsearch module could not be loaded)�__salt__�rr�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/grafana.py�__virtual__�sr	cCsPt|t�rtd|�}|sd|�d�}t|��n|}|�d�}|�d�}||fS)zI
    From a pillar key, or a dictionary, return index and host keys.
    z
config.optionzPillar key for profile z not found.�hosts�index)�
isinstance�strrr�get)�profileZ_profile�msgr
rrrr�_parse_profile�s
�

rcCsnt�|�}t�|�}|dD]	}d|vr|d=q|dD]	}d|vr%|d=qt||�}|��p6|��p6|��S)z8
    Check if grafana dashboard row and _row differ
    �panels�id)�copy�deepcopyr�changedZaddedZremoved)�row�_rowZrow_copyZ	_row_copy�panel�_panel�diffrrr�_rows_differ�s

��
rrcCs|ddid�}|s
td��|r|rtd��t|�\}}|s!td��|s)td|�}|s-g}|rO|D]}	td|	�}
t|
t�rI|
D]}|�|�q@q1|�|
�q1td||d	|d
�}|rvtd||d	|d
�}
|
�di��d	�}
tjj	�
|
�}
n|s|td
��tdr�d|�d�|d<d|d<|S|}
g}g}i}t|
d�D] \}}|dD]
}d|vr�|�|d�q�d|vr�|||d<q�|�
�|s�dg}|D]N}d|vr�td��|dD]}|�|dd�|d|d<q�|d}||vr�|�|�|
d�|�q�||}t||
d|��r||
d|<|�|�q�|�s(d|d<d|�d�|d<|Std�rCd|�d�}|�r=|�d|��}||d<|Sdd|tjj	�|
�d�}td |d	|||d!�}|�r}d|d<||d"d#<d$|�d%�}|�rw|�d&|��}||d<|Sd'|d<d(|�d%�}||d<|S))a�
    Ensure the grafana dashboard exists and is managed.

    name
        Name of the grafana dashboard.

    dashboard
        A dict that defines a dashboard that should be managed.

    dashboard_from_pillar
        A pillar key that contains a grafana dashboard dict. Mutually exclusive
        with dashboard.

    rows
        A list of grafana rows.

    rows_from_pillar
        A list of pillar keys that contain lists of grafana dashboard rows.
        Rows defined in the pillars will be appended to the rows defined in the
        state.

    profile
        A pillar key or dict that contains a list of hosts and an
        elasticsearch index to use.
    N���name�result�comment�changeszprofile is a required argument.zEdashboard and dashboard_from_pillar are mutually exclusive arguments.�'index is a required key in the profile.z
pillar.getr�	dashboard�rr�doc_typer
zelasticsearch.getZ_sourcezHGrafana dashboard does not exist and no dashboard template was provided.�test�
Dashboard z is set to be created.r!r �rowsrr�title�z!title is a required key for rows.���Tz is up to datez is set to be updated.z' The following rows set to be updated: Zguest)�user�groupr*r$zelasticsearch.index)rr&�bodyrr
r"rzUpdated dashboard �.z" The following rows were updated: FzFailed to update dashboard )rrrr�list�appendr�salt�utils�json�loads�__opts__�	enumerate�sortr�dumps)rr$Zdashboard_from_pillarr)Zrows_from_pillarr�retr
r�keyZpillar_rowsr�existsZ
_dashboardZupdate_rowsZ_ids�_dataZ_nrrrr*rr/�updatedrrr�dashboard_present�s�!�
������

�
�
��r@cCs�|ddid�}t|�\}}|std��td||d|d�}|rYtdr-d	|�d
�|d<|Std|d||d
�}|rKd|d<||dd<d|dd<|Sd|d<d|�d�|d<|Sd|d<d	|�d�|d<|S)z�
    Ensure the named grafana dashboard is deleted.

    name
        Name of the grafana dashboard.

    profile
        A pillar key or dict that contains a list of hosts and an
        elasticsearch index to use.
    Nrrr#rr$r%r'r(z is set to be removed.r!zelasticsearch.delete)rr&rr
Tr r"�old�newFzFailed to delete z dashboard.z does not exist.)rrrr7)rr
rr;rr=Zdeletedrrr�dashboard_absentms2����rC)NNNNr)Nr)
�__doc__rZsalt.utils.jsonr3Zsalt.exceptionsrZsalt.utils.dictdifferrr	rrr@rCrrrr�<module>s")	
�