> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Monitor timeseries data alert policy alignment remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the "Cloud Monitoring Alert Policies Timeseries Data Should Be Aligned" misconfiguration in GCP, you can follow the below steps using the GCP console:

        1. Open the GCP console and navigate to the Cloud Monitoring page.
        2. In the left-hand menu, click on "Alerting" and then select "Policies".
        3. Find the policy that is triggering the misconfiguration and click on it to open the policy details.
        4. In the policy details, navigate to the "Conditions" tab and find the condition that is triggering the misconfiguration.
        5. Click on the condition to open its details and scroll down to the "Alignment Period" setting.
        6. Set the "Alignment Period" to a value that aligns with the time series data being monitored. For example, if you are monitoring data that is collected every minute, set the "Alignment Period" to "1 minute".
        7. Click on "Save" to save the changes to the condition.
        8. Repeat steps 4-7 for any other conditions that are triggering the misconfiguration.
        9. Once you have updated all relevant conditions, click on "Save" to save the changes to the policy.

        By following these steps, you will have successfully remediated the "Cloud Monitoring Alert Policies Timeseries Data Should Be Aligned" misconfiguration in GCP.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Cloud Monitoring Alert Policies Timeseries Data Should Be Aligned" for GCP using GCP CLI, follow the below steps:

        1. Open the Google Cloud Console and navigate to the Cloud Monitoring page.

        2. Click on the Alerting tab on the left-hand side menu.

        3. Select the alert policy that is affected by the misconfiguration.

        4. Click on the Edit button to modify the alert policy.

        5. Scroll down to the "Conditions" section and click on the condition that is affected by the misconfiguration.

        6. In the "Configuration" section of the condition, change the "Alignment Period" to a value that aligns with the time series data.

        7. Click on the Save button to save the changes.

        8. Verify that the misconfiguration has been remediated by checking that the alert policy is no longer showing the misconfiguration.

        Alternatively, you can use the GCP CLI to remediate the misconfiguration by running the following command:

        ```
        gcloud alpha monitoring policies conditions update [CONDITION_ID] --alignment-period [ALIGNMENT_PERIOD]
        ```

        Replace `[CONDITION_ID]` with the ID of the condition that is affected by the misconfiguration, and `[ALIGNMENT_PERIOD]` with the value that aligns with the time series data.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration "Cloud Monitoring Alert Policies Timeseries Data Should Be Aligned" in GCP using Python, you can follow the below steps:

        1. First, you need to identify the alert policies that are affected by this misconfiguration. You can do this by using the GCP Cloud Monitoring API and fetching the alert policies.

        2. Once you have identified the alert policies, you need to modify them to ensure that the timeseries data is aligned. You can do this by setting the "alignmentPeriod" property of the alert policy to a value that aligns with the data being monitored.

        3. You can use the following Python code to modify the alert policies:

        ```python theme={null}
        from google.cloud import monitoring_v3

        client = monitoring_v3.AlertPolicyServiceClient()

        # Fetch all alert policies
        policies = client.list_alert_policies()

        for policy in policies:
            # Check if the policy has misconfiguration
            if policy.conditions.time_series_query.alignment_period.seconds != 60:
                # Modify the policy to set the alignment period to 60 seconds
                policy.conditions.time_series_query.alignment_period.seconds = 60
                # Update the policy
                updated_policy = client.update_alert_policy(policy)
                print(f'Updated policy: {updated_policy.name}')
        ```

        4. Once you have modified the alert policies, you should verify that the misconfiguration has been remediated by checking the alignment period of the policies.

        By following the above steps, you can remediate the misconfiguration "Cloud Monitoring Alert Policies Timeseries Data Should Be Aligned" for GCP using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "google_monitoring_alert_policy" "TIMESERIES_ALIGNED_POLICY" {
          project      = "YOUR_PROJECT_ID"                # replace with your project ID
          display_name = "YOUR_ALERT_POLICY_NAME"         # replace with your alert policy name
          combiner     = "OR"                             # or AND, as required

          conditions {
            display_name = "YOUR_CONDITION_NAME"          # replace with your condition display name

            condition_threshold {
              filter          = "YOUR_METRIC_FILTER"      # e.g. metric.type=\"compute.googleapis.com/instance/cpu/utilization\"
              comparison      = "COMPARISON_GT"          # keep your existing comparison
              threshold_value = 0.8                      # keep your existing threshold
              duration        = "300s"                   # keep your existing duration

              # Ensure timeseries data is aligned via Aggregation
              aggregations {
                alignment_period     = "60s"             # choose an alignment period appropriate to your metric / SLO
                per_series_aligner   = "ALIGN_MEAN"      # or ALIGN_RATE, ALIGN_MAX, etc. per your requirement
                cross_series_reducer = "REDUCE_NONE"     # or REDUCE_SUM, REDUCE_MEAN, etc. if you reduce across series
                group_by_fields      = []                # or ["resource.label.instance_id"], etc., if using a reducer
              }
            }
          }

          notification_channels = [
            "projects/YOUR_PROJECT_ID/notificationChannels/YOUR_CHANNEL_ID"
          ]
        }
        ```

        This change does not force replacement of the alert policy; Terraform will update it in place.

        Verification: `terraform plan` should show an in-place update to `google_monitoring_alert_policy.TIMESERIES_ALIGNED_POLICY` adding or modifying the `aggregations` block with `alignment_period` and `per_series_aligner` (and `cross_series_reducer` / `group_by_fields` if set).
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
