Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIGTOP-4299: Add basic Grafana service #121

Merged
merged 11 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
</select>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!--
~ 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.
-->

<configuration>
<property>
<name>content</name>
<description>This is the freemarker template for grafana.ini file</description>
<value><![CDATA[
# TODO
]]>
</value>
<attrs>
<type>longtext</type>
</attrs>
</property>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0"?>
<!--
~ 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.
-->

<metainfo>
<service>
<name>grafana</name>
<display-name>Grafana</display-name>
<desc>
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.
</desc>
<version>11.4.0</version>
<user>grafana</user>

<components>
<component>
<name>grafana_server</name>
<display-name>Grafana Server</display-name>
<category>server</category>
<cardinality>1+</cardinality>
</component>
</components>

<package-specifics>
<package-specific>
<architectures>
<arch>x86_64</arch>
</architectures>
<packages>
<package>
<name>grafana-11.4.0.linux-amd64.tar.gz</name>
<checksum>SHA-256:3550c73f4455435642976e82cc89aa354f076a75b766a408781107f4f5d4744c</checksum>
</package>
</packages>
</package-specific>
<package-specific>
<architectures>
<arch>aarch64</arch>
</architectures>
<packages>
<package>
<name>grafana-11.4.0.linux-arm64.tar.gz</name>
<checksum>SHA-256:c978b46a61d92883119131641c03b8a1323a284e74ab9a20e7e48207dc1a11e1</checksum>
</package>
</packages>
</package-specific>
</package-specifics>
</service>
</metainfo>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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!");
}
}
Loading