diff --git a/deploy/deploy.sh b/deploy/deploy.sh index af0b0fe8a537f2374bc340d98d2b14b9d61cb713..c76b596a816133fe0686141bba2a93e46699e553 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -1037,8 +1037,17 @@ function deploy_grafana() { fi echo -e "\n[2] Creating grafana container" + + # Create topo graph es resources + date=$(date "+%Y-%m-%d") + curl -X PUT "${ES_ADDR}/aops_graph-${date}?pretty" >/dev/null 2>&1 + docker stop ${container_name} 2>/dev/null ; docker rm ${container_name} 2>/dev/null - docker run -d --name ${container_name} --network host ${DOCKER_HUB_TAG_PREFIX}/grafana-${OS_ARCH} + docker run -d --name ${container_name} --network host \ + -e arangodb_server=${ARANGODB_ADDR} \ + -e es_server=${ES_ADDR} \ + -e prometheus_server=${PROMETHEUS_ADDR} \ + ${DOCKER_HUB_TAG_PREFIX}/grafana-${OS_ARCH} [ $? -ne 0 ] && echo_err_exit "Error: fail to run grafana container" echo -e "\n[3] Configuring datasources" @@ -1120,43 +1129,11 @@ http://admin:admin@localhost:3000/api/datasources/ 2>/dev/null) fi - # Create topo graph es resources - curl -X PUT "${ES_ADDR}/aops_graph2?pretty" >/dev/null 2>&1 - - # Running daemon that transfrom arangodb to es - [ ! -f ${WORKING_DIR}/arangodb2es.py ] && echo_err_exit "Failed to find arangodb2es.py" - if ! which pip3 >/dev/null ; then - if [ "$DEPLOY_TYPE" == "local" ] ; then - local_pip_rpm=$(ls $LOCAL_DEPLOY_SRCDIR/python3-pip*.rpm) - [ ! -f $local_pip_rpm ] && echo_err_exit "Error: failed to find local pip rpm" - yum install -y $local_pip_rpm - else - install_rpm python3-pip - fi - fi if ! which jq >/dev/null ; then install_rpm jq fi - if [ "$DEPLOY_TYPE" == "local" ] ; then - pushd $LOCAL_DEPLOY_SRCDIR - pip3 install -q elasticsearch python-arango pytz pyArango --no-index --find-links=./ - popd - else - pip3 install -q elasticsearch python-arango pytz pyArango -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com - fi - [ $? -ne 0 ] && echo_err_exit "Fail to pip install dependencies for arangodb2es.py" - - mkdir -p /opt/gala - \cp -f ${WORKING_DIR}/arangodb2es.py /opt/gala - sed -i "s/self.arangodbUrl =.*/self.arangodbUrl = 'http:\/\/${ARANGODB_ADDR}'/g" /opt/gala/arangodb2es.py - sed -i "s/self.esUrl =.*/self.esUrl = 'http:\/\/${ES_ADDR}'/g" /opt/gala/arangodb2es.py - sed -i "s/self.promethusUrl =.*/self.promethusUrl = 'http:\/\/${PROMETHEUS_ADDR}'/g" /opt/gala/arangodb2es.py - - kill -9 $(ps -ef | grep arangodb2es.py | grep -v grep | awk '{print $2}') 2>/dev/null - python3 /opt/gala/arangodb2es.py >/dev/null 2>&1 & - echo -e "\n[4] import grafana dashboard" import_grafana_dashboard diff --git a/deploy/arangodb2es.py b/grafana/arangodb2es.py similarity index 90% rename from deploy/arangodb2es.py rename to grafana/arangodb2es.py index 019a364debe4df3a8bdfe294eb55b342bb97c9e2..48722c713edc751fc08e62868a9298176f1288a9 100644 --- a/deploy/arangodb2es.py +++ b/grafana/arangodb2es.py @@ -5,6 +5,8 @@ import elasticsearch import json import sys import requests +import datetime +import os from arango import ArangoClient from elasticsearch import helpers @@ -102,7 +104,7 @@ class ElasticSearch: ] } } - resp = self.es.search(index="aops_graph2", query=query) + resp = self.es.search(index="aops_graph-*", query=query) hits = resp['hits']['hits'] if len(hits) >= 1: return True @@ -111,9 +113,24 @@ class ElasticSearch: class AOps: def __init__(self): - self.arangodbUrl = 'http://localhost:8529' - self.esUrl = 'http://localhost:9200' - self.promethusUrl = 'http://localhost:9090' + arangodbAddr = os.getenv('arangodb_server') + esAddr = os.getenv('es_server') + prometheusAddr = os.getenv('prometheus_server') + if arangodbAddr is None: + self.arangodbUrl = 'http://localhost:8529' + else: + self.arangodbUrl = 'http://'+ arangodbAddr + if esAddr is None: + self.esUrl = 'http://localhost:9200' + else: + self.esUrl = 'http://'+ esAddr + if prometheusAddr is None: + self.promethusUrl = 'http://localhost:9090' + else: + self.promethusUrl = 'http://'+ prometheusAddr + print("arangodb url ", self.arangodbUrl) + print("es url ", self.esUrl) + print("promethus url ", self.promethusUrl) self.db_client = ArangoDB(self.arangodbUrl, 'spider', 'root', '') self.es_client = ElasticSearch(self.esUrl) self.edge_collection = ['belongs_to', 'connect', 'has_vhost', 'is_peer', 'runs_on', 'store_in'] @@ -238,8 +255,21 @@ class AOps: if it['_source']['src'] in gaussdb_container_list or it['_source']['src'] in gaussdb_tcp_list: it['_source']['proc_comm'] = 'gaussdb' continue - + + def get_es_time_index(self): + time = datetime.datetime.now() + if time.hour > 23 and time.minute > 59: + time = time + datetime.timedelta(days=+1) + return time.strftime("%Y-%m-%d") + + def set_es_index(self, index): + cmd = "curl -X PUT " + self.esUrl + "/aops_graph-" + index + "?pretty"; + print(cmd) + os.system(cmd) + def fetch_graph_data(self): + time_index = self.get_es_time_index() + print(time_index) self.getHostMapFromPromethus() cause_data = self.es_client.get_cause_nodes_from_es() if cause_data['timestamp'] == sys.maxsize: @@ -254,6 +284,8 @@ class AOps: if self.es_client.has_record_in_graph(ts): print('{} has recorded'.format(ts)) return + + self.set_es_index(time_index) for edge_collection in self.edge_collection: if not self.db_client.has_collection(edge_collection): continue @@ -272,7 +304,7 @@ class AOps: edge_layer = edge['layer'] dic = { - "_index": "aops_graph2", + "_index": "aops_graph-" + time_index, "_source": { "ts": ts, "timestamp": datetime.datetime.fromtimestamp(ts / 1000, pytz.utc), diff --git a/grafana/dockerfile b/grafana/dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7ff6ca68992f25608d415b6c0515032cf58fbaf2 --- /dev/null +++ b/grafana/dockerfile @@ -0,0 +1,26 @@ +FROM grafana/grafana-oss:9.3.14-ubuntu + +WORKDIR /opt/grafana + +ADD . /opt/grafana + +USER root + +RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak +RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list +RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list + +RUN apt-get update +RUN apt-get -y install python3 python3-pip +RUN pip3 install -q elasticsearch python-arango pytz pyArango -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com + +RUN grafana-cli plugins install pyroscope-datasource \ + && grafana-cli plugins install pyroscope-panel \ + && grafana-cli plugins install marcusolsson-gantt-panel \ + && cp -rf ./aops-sdg-panel /var/lib/grafana/plugins \ + && sed -i 's/^;allow_loading_unsigned_plugins.*/allow_loading_unsigned_plugins = aops-sdg-panel/g' /etc/grafana/grafana.ini + +RUN sed -i "2i python3 /opt/grafana/arangodb2es.py > /opt/grafana/arangodb2es.log &" /run.sh + + +