new-integration-tests/src/test/java/oracle/weblogic/kubernetes/ItMonitoringExporter.java
private void installPrometheusGrafana(String promChartVersion,
                                        String grafanaChartVersion,
                                        String domainNS,
                                        String domainUid
                                        ) throws IOException, ApiException {
    final String prometheusRegexValue = String.format("regex: %s;%s;%s", domainNS, domainUid, clusterName);
    if (promHelmParams == null) {
      logger.info("create a staging location for monitoring creation scripts");
      Path fileTemp = Paths.get(RESULTS_ROOT, "ItMonitoringExporter", "createTempValueFile");
      FileUtils.deleteDirectory(fileTemp.toFile());
      Files.createDirectories(fileTemp);
  
      logger.info("copy the promvalue.yaml to staging location");
      Path srcPromFile = Paths.get(RESOURCE_DIR, "exporter", "promvalues.yaml");
      Path targetPromFile = Paths.get(fileTemp.toString(), "promvalues.yaml");
      Files.copy(srcPromFile, targetPromFile, StandardCopyOption.REPLACE_EXISTING);
      String oldValue = "regex: default;domain1;cluster-1";
      replaceStringInFile(targetPromFile.toString(),
              oldValue,
              prometheusRegexValue);
      //replace with webhook ns
      replaceStringInFile(targetPromFile.toString(),
              "webhook.webhook.svc.cluster.local",
              String.format("webhook.%s.svc.cluster.local", webhookNS));

    
      nodeportserver = getNextFreePort(32400, 32600);
      int nodeportalertmanserver = getNextFreePort(30400, 30600);
      promHelmParams = installAndVerifyPrometheus("prometheus",
              monitoringNS,
              targetPromFile.toString(),
              promChartVersion,
              nodeportserver,
              nodeportalertmanserver);
    
      prometheusDomainRegexValue = prometheusRegexValue;
    }
    //if prometheus already installed change CM for specified domain
    if (!prometheusRegexValue.equals(prometheusDomainRegexValue)) {
      logger.info("update prometheus Config Map with domain info");
      editPrometheusCM(prometheusDomainRegexValue, prometheusRegexValue);
      prometheusDomainRegexValue = prometheusRegexValue;
    }
    logger.info("Prometheus is running");

    String testappPrometheusSearchKey =
            "wls_servlet_invocation_total_count%7Bapp%3D%22wls-exporter%22%7D%5B15s%5D";

    assertDoesNotThrow(() -> checkMetricsViaPrometheus(testappPrometheusSearchKey, "wls-exporter"));


    if (grafanaHelmParams == null) {
      int nodeportgrafana = getNextFreePort(31000, 31200);
      grafanaHelmParams = installAndVerifyGrafana("grafana",
              monitoringNS,
              monitoringExporterEndToEndDir + "/grafana/values.yaml",
              grafanaChartVersion,
              nodeportgrafana);

      //wait until it starts dashboard
      String curlCmd = String.format("curl -v  -H 'Content-Type: application/json' "
                      + " -X GET http://admin:12345678@%s:%s/api/dashboards",
              K8S_NODEPORT_HOST, nodeportgrafana);
      withStandardRetryPolicy
              .conditionEvaluationListener(
                condition -> logger.info("Check access to grafana dashboard  "
                                      + "(elapsed time {0}ms, remaining time {1}ms)",
                              condition.getElapsedTimeInMS(),
                              condition.getRemainingTimeInMS()))
              .until(assertDoesNotThrow(() -> searchForKey(curlCmd, "grafana"),
                      String.format("Check access to grafana dashboard"
                              )));
      logger.info("installing grafana dashboard");
      // url
      String curlCmd0 =
              String.format("curl -v -H 'Content-Type: application/json' -H \"Content-Type: application/json\""
                              + "  -X POST http://admin:12345678@%s:%s/api/datasources/"
                              + "  --data-binary @%sgrafana/datasource.json",
                      K8S_NODEPORT_HOST, nodeportgrafana, monitoringExporterEndToEndDir);

      logger.info("Executing Curl cmd {0}", curlCmd);
      assertDoesNotThrow(() -> ExecCommand.exec(curlCmd0));

      String curlCmd1 =
              String.format("curl -v -H 'Content-Type: application/json' -H \"Content-Type: application/json\""
                              + "  -X POST http://admin:12345678@%s:%s/api/dashboards/db/"
                              + "  --data-binary @%sgrafana/dashboard.json",
                      K8S_NODEPORT_HOST, nodeportgrafana, monitoringExporterEndToEndDir);
      logger.info("Executing Curl cmd {0}", curlCmd1);
      assertDoesNotThrow(() -> ExecCommand.exec(curlCmd1));

      String curlCmd2 = String.format("curl -v  -H 'Content-Type: application/json' "
                      + " -X GET http://admin:12345678@%s:%s/api/dashboards/db/weblogic-server-dashboard",
              K8S_NODEPORT_HOST, nodeportgrafana);
      withStandardRetryPolicy
              .conditionEvaluationListener(
                  condition -> logger.info("Check grafana dashboard metric against expected {0} "
                                  + "(elapsed time {2}ms, remaining time {3}ms)",
                          "wls_jvm_uptime",
                          condition.getElapsedTimeInMS(),
                          condition.getRemainingTimeInMS()))
              .until(assertDoesNotThrow(() -> searchForKey(curlCmd2, "wls_jvm_uptime"),
                      String.format("Check grafana dashboard wls against expected %s",
                              "wls_jvm_uptime")));
    }
    logger.info("Grafana is running");
  }
/**
   * Install Prometheus, Grafana using specified helm chart version, and verify that pods are running.
   * @throws ApiException when creating helm charts or pods fails
   */
Install Prometheus, Grafana using specified helm chart version, and verify that pods are running.
@throws ApiException when creating helm charts or pods fails
Install Prometheus Grafana using specified helm chart version and verify that pods are running . @throws ApiException when creating helm charts or pods fails
private void installPrometheusGrafana(String promChartVersion,
                                        String grafanaChartVersion,
                                        String domainNS,
                                        String domainUid
                                        ) throws IOException, ApiException {
    final String prometheusRegexValue = String.format("regex: %s;%s;%s", domainNS, domainUid, clusterName);
    if (promHelmParams == null) {
      logger.info("create a staging location for monitoring creation scripts");
      Path fileTemp = Paths.get(RESULTS_ROOT, "ItMonitoringExporter", "createTempValueFile");
      FileUtils.deleteDirectory(fileTemp.toFile());
      Files.createDirectories(fileTemp);
      logger.info("copy the promvalue.yaml to staging location");
      Path srcPromFile = Paths.get(RESOURCE_DIR, "exporter", "promvalues.yaml");
      Path targetPromFile = Paths.get(fileTemp.toString(), "promvalues.yaml");
      Files.copy(srcPromFile, targetPromFile, StandardCopyOption.REPLACE_EXISTING);
      String oldValue = "regex: default;domain1;cluster-1";
      replaceStringInFile(targetPromFile.toString(),
              oldValue,
              prometheusRegexValue);
      replaceStringInFile(targetPromFile.toString(),
              "webhook.webhook.svc.cluster.local",
              String.format("webhook.%s.svc.cluster.local", webhookNS));
      nodeportserver = getNextFreePort(32400, 32600);
      int nodeportalertmanserver = getNextFreePort(30400, 30600);
      promHelmParams = installAndVerifyPrometheus("prometheus",
              monitoringNS,
              targetPromFile.toString(),
              promChartVersion,
              nodeportserver,
              nodeportalertmanserver);
      prometheusDomainRegexValue = prometheusRegexValue;
    }
    if (!prometheusRegexValue.equals(prometheusDomainRegexValue)) {
      logger.info("update prometheus Config Map with domain info");
      editPrometheusCM(prometheusDomainRegexValue, prometheusRegexValue);
      prometheusDomainRegexValue = prometheusRegexValue;
    }
    logger.info("Prometheus is running");
    String testappPrometheusSearchKey =
            "wls_servlet_invocation_total_count%7Bapp%3D%22wls-exporter%22%7D%5B15s%5D";
    assertDoesNotThrow(() -> checkMetricsViaPrometheus(testappPrometheusSearchKey, "wls-exporter"));
    if (grafanaHelmParams == null) {
      int nodeportgrafana = getNextFreePort(31000, 31200);
      grafanaHelmParams = installAndVerifyGrafana("grafana",
              monitoringNS,
              monitoringExporterEndToEndDir + "/grafana/values.yaml",
              grafanaChartVersion,
              nodeportgrafana);
      String curlCmd = String.format("curl -v  -H 'Content-Type: application/json' "
                      + " -X GET http://admin:12345678@%s:%s/api/dashboards",
              K8S_NODEPORT_HOST, nodeportgrafana);
      withStandardRetryPolicy
              .conditionEvaluationListener(
                condition -> logger.info("Check access to grafana dashboard  "
                                      + "(elapsed time {0}ms, remaining time {1}ms)",
                              condition.getElapsedTimeInMS(),
                              condition.getRemainingTimeInMS()))
              .until(assertDoesNotThrow(() -> searchForKey(curlCmd, "grafana"),
                      String.format("Check access to grafana dashboard"
                              )));
      logger.info("installing grafana dashboard");
      String curlCmd0 =
              String.format("curl -v -H 'Content-Type: application/json' -H \"Content-Type: application/json\""
                              + "  -X POST http://admin:12345678@%s:%s/api/datasources/"
                              + "  --data-binary @%sgrafana/datasource.json",
                      K8S_NODEPORT_HOST, nodeportgrafana, monitoringExporterEndToEndDir);
      logger.info("Executing Curl cmd {0}", curlCmd);
      assertDoesNotThrow(() -> ExecCommand.exec(curlCmd0));
      String curlCmd1 =
              String.format("curl -v -H 'Content-Type: application/json' -H \"Content-Type: application/json\""
                              + "  -X POST http://admin:12345678@%s:%s/api/dashboards/db/"
                              + "  --data-binary @%sgrafana/dashboard.json",
                      K8S_NODEPORT_HOST, nodeportgrafana, monitoringExporterEndToEndDir);
      logger.info("Executing Curl cmd {0}", curlCmd1);
      assertDoesNotThrow(() -> ExecCommand.exec(curlCmd1));
      String curlCmd2 = String.format("curl -v  -H 'Content-Type: application/json' "
                      + " -X GET http://admin:12345678@%s:%s/api/dashboards/db/weblogic-server-dashboard",
              K8S_NODEPORT_HOST, nodeportgrafana);
      withStandardRetryPolicy
              .conditionEvaluationListener(
                  condition -> logger.info("Check grafana dashboard metric against expected {0} "
                                  + "(elapsed time {2}ms, remaining time {3}ms)",
                          "wls_jvm_uptime",
                          condition.getElapsedTimeInMS(),
                          condition.getRemainingTimeInMS()))
              .until(assertDoesNotThrow(() -> searchForKey(curlCmd2, "wls_jvm_uptime"),
                      String.format("Check grafana dashboard wls against expected %s",
                              "wls_jvm_uptime")));
    }
    logger.info("Grafana is running");
  }
private void installPrometheusGrafana ( String promChartVersion , String grafanaChartVersion , String domainNS , String domainUid ) throws IOException , ApiException { final String prometheusRegexValue = String . format ( "regex: %s;%s;%s" , domainNS , domainUid , clusterName ) ; if ( promHelmParams == null ) { logger . info ( "create a staging location for monitoring creation scripts" ) ; Path fileTemp = Paths . get ( RESULTS_ROOT , "ItMonitoringExporter" , "createTempValueFile" ) ; FileUtils . deleteDirectory ( fileTemp . toFile ( ) ) ; Files . createDirectories ( fileTemp ) ; logger . info ( "copy the promvalue.yaml to staging location" ) ; Path srcPromFile = Paths . get ( RESOURCE_DIR , "exporter" , "promvalues.yaml" ) ; Path targetPromFile = Paths . get ( fileTemp . toString ( ) , "promvalues.yaml" ) ; Files . copy ( srcPromFile , targetPromFile , StandardCopyOption . REPLACE_EXISTING ) ; String oldValue = "regex: default;domain1;cluster-1" ; replaceStringInFile ( targetPromFile . toString ( ) , oldValue , prometheusRegexValue ) ; replaceStringInFile ( targetPromFile . toString ( ) , "webhook.webhook.svc.cluster.local" , String . format ( "webhook.%s.svc.cluster.local" , webhookNS ) ) ; nodeportserver = getNextFreePort ( 32400 , 32600 ) ; int nodeportalertmanserver = getNextFreePort ( 30400 , 30600 ) ; promHelmParams = installAndVerifyPrometheus ( "prometheus" , monitoringNS , targetPromFile . toString ( ) , promChartVersion , nodeportserver , nodeportalertmanserver ) ; prometheusDomainRegexValue = prometheusRegexValue ; } if ( ! prometheusRegexValue . equals ( prometheusDomainRegexValue ) ) { logger . info ( "update prometheus Config Map with domain info" ) ; editPrometheusCM ( prometheusDomainRegexValue , prometheusRegexValue ) ; prometheusDomainRegexValue = prometheusRegexValue ; } logger . info ( "Prometheus is running" ) ; String testappPrometheusSearchKey = "wls_servlet_invocation_total_count%7Bapp%3D%22wls-exporter%22%7D%5B15s%5D" ; assertDoesNotThrow ( ( ) -> checkMetricsViaPrometheus ( testappPrometheusSearchKey , "wls-exporter" ) ) ; if ( grafanaHelmParams == null ) { int nodeportgrafana = getNextFreePort ( 31000 , 31200 ) ; grafanaHelmParams = installAndVerifyGrafana ( "grafana" , monitoringNS , monitoringExporterEndToEndDir + "/grafana/values.yaml" , grafanaChartVersion , nodeportgrafana ) ; String curlCmd = String . format ( "curl -v  -H 'Content-Type: application/json' " + " -X GET http://admin:12345678@%s:%s/api/dashboards" , K8S_NODEPORT_HOST , nodeportgrafana ) ; withStandardRetryPolicy . conditionEvaluationListener ( condition -> logger . info ( "Check access to grafana dashboard  " + "(elapsed time {0}ms, remaining time {1}ms)" , condition . getElapsedTimeInMS ( ) , condition . getRemainingTimeInMS ( ) ) ) . until ( assertDoesNotThrow ( ( ) -> searchForKey ( curlCmd , "grafana" ) , String . format ( "Check access to grafana dashboard" ) ) ) ; logger . info ( "installing grafana dashboard" ) ; String curlCmd0 = String . format ( "curl -v -H 'Content-Type: application/json' -H \"Content-Type: application/json\"" + "  -X POST http://admin:12345678@%s:%s/api/datasources/" + "  --data-binary @%sgrafana/datasource.json" , K8S_NODEPORT_HOST , nodeportgrafana , monitoringExporterEndToEndDir ) ; logger . info ( "Executing Curl cmd {0}" , curlCmd ) ; assertDoesNotThrow ( ( ) -> ExecCommand . exec ( curlCmd0 ) ) ; String curlCmd1 = String . format ( "curl -v -H 'Content-Type: application/json' -H \"Content-Type: application/json\"" + "  -X POST http://admin:12345678@%s:%s/api/dashboards/db/" + "  --data-binary @%sgrafana/dashboard.json" , K8S_NODEPORT_HOST , nodeportgrafana , monitoringExporterEndToEndDir ) ; logger . info ( "Executing Curl cmd {0}" , curlCmd1 ) ; assertDoesNotThrow ( ( ) -> ExecCommand . exec ( curlCmd1 ) ) ; String curlCmd2 = String . format ( "curl -v  -H 'Content-Type: application/json' " + " -X GET http://admin:12345678@%s:%s/api/dashboards/db/weblogic-server-dashboard" , K8S_NODEPORT_HOST , nodeportgrafana ) ; withStandardRetryPolicy . conditionEvaluationListener ( condition -> logger . info ( "Check grafana dashboard metric against expected {0} " + "(elapsed time {2}ms, remaining time {3}ms)" , "wls_jvm_uptime" , condition . getElapsedTimeInMS ( ) , condition . getRemainingTimeInMS ( ) ) ) . until ( assertDoesNotThrow ( ( ) -> searchForKey ( curlCmd2 , "wls_jvm_uptime" ) , String . format ( "Check grafana dashboard wls against expected %s" , "wls_jvm_uptime" ) ) ) ; } logger . info ( "Grafana is running" ) ; }
Install Prometheus, Grafana using specified helm chart version, and verify that pods are running.
Install Prometheus Grafana using specified helm chart version and verify that pods are running .
//replace with webhook ns //if prometheus already installed change CM for specified domain //wait until it starts dashboard // url