From 24cc1e085d205acb58bae897b1baee7ba027426c Mon Sep 17 00:00:00 2001 From: haopeng <657407891@qq.com> Date: Wed, 11 Dec 2024 13:50:35 +0800 Subject: [PATCH] BIGTOP-4299: Add basic Grafana service (#121) --- .../mapper/postgresql/ClusterMapper.xml | 3 +- .../grafana/configuration/grafana.xml | 33 ++++++ .../infra/1.0.0/services/grafana/metainfo.xml | 67 +++++++++++ .../infra/1.0.0/services/grafana/order.json | 1 + .../infra/v1_0_0/grafana/GrafanaParams.java | 50 ++++++++ .../v1_0_0/grafana/GrafanaServerScript.java | 111 ++++++++++++++++++ .../infra/v1_0_0/grafana/GrafanaSetup.java | 43 +++++++ 7 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/configuration/grafana.xml create mode 100644 bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/metainfo.xml create mode 100644 bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/order.json create mode 100644 bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaParams.java create mode 100644 bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaServerScript.java create mode 100644 bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaSetup.java diff --git a/bigtop-manager-dao/src/main/resources/mapper/postgresql/ClusterMapper.xml b/bigtop-manager-dao/src/main/resources/mapper/postgresql/ClusterMapper.xml index 7d72590c1..829713c68 100644 --- a/bigtop-manager-dao/src/main/resources/mapper/postgresql/ClusterMapper.xml +++ b/bigtop-manager-dao/src/main/resources/mapper/postgresql/ClusterMapper.xml @@ -38,10 +38,11 @@ count(h.id) as total_host, sum(h.available_processors) as total_processor, sum(h.total_memory_size) as total_memory, sum(h.total_disk) as total_disk, count(s.id) as total_service from - cluster c left join user u on c.create_by = u.id + cluster c left join "user" u on c.create_by = u.id left join host h on c.id = h.cluster_id left join service s on c.id = s.cluster_id where c.id = #{id} + group by c.id, u.nickname limit 1 diff --git a/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/configuration/grafana.xml b/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/configuration/grafana.xml new file mode 100644 index 000000000..d15db89ee --- /dev/null +++ b/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/configuration/grafana.xml @@ -0,0 +1,33 @@ + + + + + + content + This is the freemarker template for grafana.ini file + + + + longtext + + + \ No newline at end of file diff --git a/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/metainfo.xml b/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/metainfo.xml new file mode 100644 index 000000000..1670a9bb0 --- /dev/null +++ b/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/metainfo.xml @@ -0,0 +1,67 @@ + + + + + + grafana + Grafana + + The open and composable observability and data visualization platform. + Visualize metrics, logs, and traces from multiple sources like Prometheus, + Loki, Elasticsearch, InfluxDB, Postgres and many more. + + 11.4.0 + grafana + + + + grafana_server + Grafana Server + server + 1+ + + + + + + + x86_64 + + + + grafana-11.4.0.linux-amd64.tar.gz + SHA-256:3550c73f4455435642976e82cc89aa354f076a75b766a408781107f4f5d4744c + + + + + + aarch64 + + + + grafana-11.4.0.linux-arm64.tar.gz + SHA-256:c978b46a61d92883119131641c03b8a1323a284e74ab9a20e7e48207dc1a11e1 + + + + + + \ No newline at end of file diff --git a/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/order.json b/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/order.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/bigtop-manager-server/src/main/resources/stacks/infra/1.0.0/services/grafana/order.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaParams.java b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaParams.java new file mode 100644 index 000000000..6a9f1c063 --- /dev/null +++ b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaParams.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.stack.infra.v1_0_0.grafana; + +import org.apache.bigtop.manager.common.message.entity.payload.CommandPayload; +import org.apache.bigtop.manager.stack.core.spi.param.Params; +import org.apache.bigtop.manager.stack.infra.param.InfraParams; + +import com.google.auto.service.AutoService; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.text.MessageFormat; + +@Getter +@Slf4j +@AutoService(Params.class) +@NoArgsConstructor +public class GrafanaParams extends InfraParams { + + public GrafanaParams(CommandPayload commandPayload) { + super(commandPayload); + } + + public String dataDir() { + return MessageFormat.format("{0}/data", serviceHome()); + } + + @Override + public String getServiceName() { + return "grafana"; + } +} diff --git a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaServerScript.java b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaServerScript.java new file mode 100644 index 000000000..23209b8d5 --- /dev/null +++ b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaServerScript.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.stack.infra.v1_0_0.grafana; + +import org.apache.bigtop.manager.common.shell.ShellResult; +import org.apache.bigtop.manager.stack.core.exception.StackException; +import org.apache.bigtop.manager.stack.core.spi.param.Params; +import org.apache.bigtop.manager.stack.core.spi.script.AbstractServerScript; +import org.apache.bigtop.manager.stack.core.spi.script.Script; +import org.apache.bigtop.manager.stack.core.utils.linux.LinuxOSUtils; + +import com.google.auto.service.AutoService; +import lombok.extern.slf4j.Slf4j; + +import java.text.MessageFormat; +import java.util.Properties; + +@AutoService(Script.class) +@Slf4j +public class GrafanaServerScript extends AbstractServerScript { + + @Override + public ShellResult add(Params params) { + Properties properties = new Properties(); + properties.setProperty(PROPERTY_KEY_SKIP_LEVELS, "1"); + + return super.add(params, properties); + } + + @Override + public ShellResult configure(Params params) { + return GrafanaSetup.config(params); + } + + @Override + public ShellResult start(Params params) { + configure(params); + GrafanaParams grafanaParams = (GrafanaParams) params; + String cmd = MessageFormat.format( + "nohup {0}/bin/grafana server --homepath {0} > {0}/nohup.out 2>&1 &", grafanaParams.serviceHome()); + log.info(cmd); + try { + ShellResult shellResult = LinuxOSUtils.sudoExecCmd(cmd, grafanaParams.user()); + if (shellResult.getExitCode() != 0) { + throw new StackException("Failed to start Grafana: {0}", shellResult.getErrMsg()); + } + long startTime = System.currentTimeMillis(); + long maxWaitTime = 5000; + long pollInterval = 500; + + while (System.currentTimeMillis() - startTime < maxWaitTime) { + ShellResult statusResult = status(params); + if (statusResult.getExitCode() == 0) { + return statusResult; + } + Thread.sleep(pollInterval); + } + return status(params); + } catch (Exception e) { + throw new StackException(e); + } + } + + @Override + public ShellResult stop(Params params) { + GrafanaParams grafanaParams = (GrafanaParams) params; + String cmd = MessageFormat.format("pkill -f {0}/bin/grafana", grafanaParams.serviceHome()); + try { + return LinuxOSUtils.sudoExecCmd(cmd, grafanaParams.user()); + } catch (Exception e) { + throw new StackException(e); + } + } + + @Override + public ShellResult status(Params params) { + GrafanaParams grafanaParams = (GrafanaParams) params; + String cmd = MessageFormat.format("pgrep -f {0}/bin/grafana", grafanaParams.serviceHome()); + try { + ShellResult result = LinuxOSUtils.execCmd(cmd); + if (result.getExitCode() == 0) { + return ShellResult.success(); + } else { + return new ShellResult(-1, "", "Grafana is not running"); + } + } catch (Exception e) { + throw new StackException(e); + } + } + + @Override + public String getComponentName() { + return "grafana_server"; + } +} diff --git a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaSetup.java b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaSetup.java new file mode 100644 index 000000000..7a61555cf --- /dev/null +++ b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/grafana/GrafanaSetup.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.stack.infra.v1_0_0.grafana; + +import org.apache.bigtop.manager.common.constants.Constants; +import org.apache.bigtop.manager.common.shell.ShellResult; +import org.apache.bigtop.manager.stack.core.spi.param.Params; +import org.apache.bigtop.manager.stack.core.utils.linux.LinuxFileUtils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class GrafanaSetup { + + public static ShellResult config(Params params) { + GrafanaParams grafanaParams = (GrafanaParams) params; + String user = grafanaParams.user(); + String group = grafanaParams.group(); + + LinuxFileUtils.createDirectories(grafanaParams.dataDir(), user, group, Constants.PERMISSION_755, true); + + return ShellResult.success("Grafana Configure success!"); + } +}