-- agent_install_file_info
UPDATE public.agent_install_file_info SET yaml = '---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cloudmoa-metric-agent-config
  namespace: $CLOUDMOA_NAMESPACE
data:
  scaling.rules: |
    groups:
    - name: scaleup
      rules :
      - alert : ScaleUpRule
        expr: job:webapp_config_open_sessions_current_count:sum > 15
        annotations:
          summary: "Scale up when current sessions is greater than 15"
          description: "Firing when total sessions active greater than 15"
  metric-agent.yml: |
    global:
      scrape_interval:     15s

    scrape_configs:
      - job_name: ''kubernetes-kubelet''
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node
        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: __address__
          replacement: kubernetes.default.svc:443
        - source_labels: [__meta_kubernetes_node_name]
          regex: (.+)
          target_label: __metrics_path__
          replacement: /api/v1/nodes/${1}/proxy/metrics
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_clst_id
          replacement: ''$CLOUDMOA_CLUSTER_ID''
        - target_label: xm_entity_type
          replacement: ''Node''

        metric_relabel_configs:
        - source_labels: [ __name__ ]
          regex: (kubelet_running_pod_count)
          action: keep

      - job_name: ''kubernetes-node-exporter''
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node

        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - source_labels: [__meta_kubernetes_role]
          action: replace
          target_label: kubernetes_role
        - source_labels: [__address__]
          regex: ''(.*):10250''
          replacement: ''${1}:9110''
          target_label: __address__
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: __instance__
          # set "name" value to "job"
        - source_labels: [job]
          regex: ''kubernetes-(.*)''
          replacement: ''${1}''
          target_label: name
        - target_label: xm_clst_id
          replacement: ''$CLOUDMOA_CLUSTER_ID''
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_entity_type
          replacement: ''Node''
        - source_labels: [__meta_kubernetes_namespace]
          separator: ;
          regex: (.*)
          target_label: xm_namespace
          replacement: $1
          action: replace
        metric_relabel_configs:
        - source_labels: [ __name__ ]
          regex: (node_memory_SReclaimable_bytes|node_boot_time_seconds|node_context_switches_total|node_cpu_frequency_max_hertz|node_cpu_package_throttles_total|node_cpu_seconds_total|node_disk_io_time_seconds_total|node_disk_read_bytes_total|node_disk_read_time_seconds_total|node_disk_reads_completed_total|node_disk_write_time_seconds_total|node_disk_writes_completed_total|node_disk_written_bytes_total|node_filefd_allocated|node_filesystem_avail_bytes|node_filesystem_free_bytes|node_filesystem_size_bytes|node_load1|node_load15|node_load5|node_memory_Active_bytes|node_memory_Buffers_bytes|node_memory_Cached_bytes|node_memory_MemAvailable_bytes|node_memory_MemFree_bytes|node_memory_MemTotal_bytes|node_memory_SwapCached_bytes|node_memory_SwapFree_bytes|node_memory_SwapTotal_bytes|node_network_receive_bytes_total|node_network_receive_bytes_total|node_network_transmit_bytes_total|node_network_transmit_bytes_total)
          action: keep


      - job_name: ''kubernetes-cadvisor''
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          insecure_skip_verify: true
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node
        metrics_path: /metrics/cadvisor

        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: xm_clst_id
          replacement: ''$CLOUDMOA_CLUSTER_ID''
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_entity_type
          replacement: ''Container''

        metric_relabel_configs:
        - source_labels: [namespace]
          target_label: xm_namespace
        - source_labels: [pod_name]
          target_label: xm_pod_id
        - source_labels: [container_name]
          target_label: xm_cont_name
        - source_labels: [id]
          target_label: xm_cont_id
        - source_labels: [container_name]
          regex: (.+)
          action: keep
        - source_labels: [ __name__ ]
          regex: (container_cpu_cfs_throttled_seconds_total|container_cpu_system_seconds_total|container_cpu_usage_seconds_total|container_cpu_user_seconds_total|container_fs_limit_bytes|container_fs_reads_bytes_total|container_fs_usage_bytes|container_fs_writes_bytes_total|container_last_seen|container_memory_cache|container_memory_max_usage_bytes|container_memory_swap|container_memory_usage_bytes|container_memory_working_set_bytes|container_network_receive_bytes_total|container_network_transmit_bytes_total|container_spec_memory_limit_bytes)
          action: keep
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudmoa-metric-agent
  namespace: $CLOUDMOA_NAMESPACE
  labels:
    app: cloudmoa-metric-agent
spec:
  selector:
    matchLabels:
      app: cloudmoa-metric-agent
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: cloudmoa-metric-agent
    spec:
      containers:
        - name: cloudmoa-metric-agent
          image: $DOCKER_REGISTRY_URL/metric-agent:$IMAGE_TAG
          args:
            - --config.file=/etc/metric-agent/metric-agent.yml
          resources:
            requests:
              cpu: 100m
              memory: 512Mi
            limits:
              cpu: 300m
              memory: 1000Mi
          volumeMounts:
            - mountPath: /etc/metric-agent/
              name: config-volume
          env:
            - name: LOG_LEVEL
              value: "INFO"
            - name: CLUSTER_ID
              value: $CLOUDMOA_CLUSTER_ID
            - name: DATAGATE
              value: $COLLTION_SERVER_DATAGATE_IP:$COLLTION_SERVER_DATAGATE_PORT
            - name: STORAGE_TYPE
              value: datagate
      restartPolicy: Always
      volumes:
        - name: config-volume
          configMap:
            name: cloudmoa-metric-agent-config
'::text WHERE id = 3::bigint;

UPDATE public.agent_install_file_info SET yaml = '---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cloudmoa-metric-agent-config
  namespace: $CLOUDMOA_NAMESPACE
data:
  scaling.rules: |
    groups:
    - name: scaleup
      rules :
      - alert : ScaleUpRule
        expr: job:webapp_config_open_sessions_current_count:sum > 15
        annotations:
          summary: "Scale up when current sessions is greater than 15"
          description: "Firing when total sessions active greater than 15"
  metric-agent.yml: |
    global:
      scrape_interval:     15s

    scrape_configs:
      - job_name: ''kubernetes-kubelet''
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node
        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: __address__
          replacement: kubernetes.default.svc:443
        - source_labels: [__meta_kubernetes_node_name]
          regex: (.+)
          target_label: __metrics_path__
          replacement: /api/v1/nodes/${1}/proxy/metrics
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_clst_id
          replacement: ''$CLOUDMOA_CLUSTER_ID''
        - target_label: xm_entity_type
          replacement: ''Node''
        metric_relabel_configs:
        - source_labels: [ __name__ ]
          regex: (kubelet_running_pod_count)
          action: keep

      - job_name: ''kubernetes-node-exporter''
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node

        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - source_labels: [__meta_kubernetes_role]
          action: replace
          target_label: kubernetes_role
        - source_labels: [__address__]
          regex: ''(.*):10250''
          replacement: ''${1}:9110''
          target_label: __address__
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: __instance__
          # set "name" value to "job"
        - source_labels: [job]
          regex: ''kubernetes-(.*)''
          replacement: ''${1}''
          target_label: name
        - target_label: xm_clst_id
          replacement: ''$CLOUDMOA_CLUSTER_ID''
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_entity_type
          replacement: ''Node''
        - source_labels: [__meta_kubernetes_namespace]
          separator: ;
          regex: (.*)
          target_label: xm_namespace
          replacement: $1
          action: replace
        metric_relabel_configs:
        - source_labels: [ __name__ ]
          regex: (node_memory_SReclaimable_bytes|node_boot_time_seconds|node_context_switches_total|node_cpu_frequency_max_hertz|node_cpu_package_throttles_total|node_cpu_seconds_total|node_disk_io_time_seconds_total|node_disk_read_bytes_total|node_disk_read_time_seconds_total|node_disk_reads_completed_total|node_disk_write_time_seconds_total|node_disk_writes_completed_total|node_disk_written_bytes_total|node_filefd_allocated|node_filesystem_avail_bytes|node_filesystem_free_bytes|node_filesystem_size_bytes|node_load1|node_load15|node_load5|node_memory_Active_bytes|node_memory_Buffers_bytes|node_memory_Cached_bytes|node_memory_MemAvailable_bytes|node_memory_MemFree_bytes|node_memory_MemTotal_bytes|node_memory_SwapCached_bytes|node_memory_SwapFree_bytes|node_memory_SwapTotal_bytes|node_network_receive_bytes_total|node_network_receive_bytes_total|node_network_transmit_bytes_total|node_network_transmit_bytes_total)
          action: keep

      - job_name: ''kubernetes-cadvisor''
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          insecure_skip_verify: true
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token

        kubernetes_sd_configs:
        - role: node
        metrics_path: /metrics/cadvisor

        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: xm_clst_id
          replacement: ''$CLOUDMOA_CLUSTER_ID''
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_entity_type
          replacement: ''Container''

        metric_relabel_configs:
        - source_labels: [namespace]
          target_label: xm_namespace
        - source_labels: [pod]
          target_label: xm_pod_id
        - source_labels: [container]
          target_label: xm_cont_name
        - source_labels: [id]
          target_label: xm_cont_id
        - source_labels: [container]
          regex: (.+)
          action: keep
        - source_labels: [ __name__ ]
          regex: (container_cpu_cfs_throttled_seconds_total|container_cpu_system_seconds_total|container_cpu_usage_seconds_total|container_cpu_user_seconds_total|container_fs_limit_bytes|container_fs_reads_bytes_total|container_fs_usage_bytes|container_fs_writes_bytes_total|container_last_seen|container_memory_cache|container_memory_max_usage_bytes|container_memory_swap|container_memory_usage_bytes|container_memory_working_set_bytes|container_network_receive_bytes_total|container_network_transmit_bytes_total|container_spec_memory_limit_bytes)
          action: keep
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudmoa-metric-agent
  namespace: $CLOUDMOA_NAMESPACE
  labels:
    app: cloudmoa-metric-agent
spec:
  selector:
    matchLabels:
      app: cloudmoa-metric-agent
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: cloudmoa-metric-agent
    spec:
      containers:
        - name: cloudmoa-metric-agent
          image: $DOCKER_REGISTRY_URL/metric-agent:$IMAGE_TAG
          args:
            - --config.file=/etc/metric-agent/metric-agent.yml
          resources:
            requests:
              cpu: 100m
              memory: 512Mi
            limits:
              cpu: 300m
              memory: 1000Mi
          volumeMounts:
            - mountPath: /etc/metric-agent/
              name: config-volume
          env:
            - name: LOG_LEVEL
              value: "INFO"
            - name: LOG_MAXAGE
              value: "1"
            - name: CLUSTER_ID
              value: $CLOUDMOA_CLUSTER_ID
            - name: DATAGATE
              value: $COLLTION_SERVER_DATAGATE_IP:$COLLTION_SERVER_DATAGATE_PORT
            - name: STORAGE_TYPE
              value: datagate
      restartPolicy: Always
      volumes:
        - name: config-volume
          configMap:
            name: cloudmoa-metric-agent-config
'::text WHERE id = 6::bigint;

-- CLOUD-2798 pod_phase_count_by_cluster metric_meta 수정
UPDATE metric_meta2 SET expr = 'count by(xm_clst_id, pod_state) (sum by (xm_clst_id, xm_pod_id, pod_state)(rate(imxc_kubernetes_container_resource_limit_cpu{{filter}}[1m])))' WHERE id = 'pod_phase_count_by_cluster';

-- node_memory_usage 수정
update metric_meta2 set expr = 'sum by (xm_node_id)((node_memory_MemTotal_bytes{xm_entity_type="Node"}- (node_memory_MemFree_bytes{xm_entity_type="Node"} + node_memory_Cached_bytes{xm_entity_type="Node"} + node_memory_Buffers_bytes{xm_entity_type="Node"})) >= 0 or node_memory_MemTotal_bytes{xm_entity_type="Node"}- node_memory_MemFree_bytes{xm_entity_type="Node"}) / (sum by (xm_node_id) (imxc_kubernetes_node_resource_capacity_memory{{filter}})) * 100' where id = 'node_memory_usage';