Skip to content

Commit

Permalink
Added Hazelcast statistics (if isHazelcast=True)
Browse files Browse the repository at this point in the history
  • Loading branch information
swifty94 committed Aug 13, 2021
1 parent 12b3c0d commit 8c90696
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 51 deletions.
69 changes: 47 additions & 22 deletions controllers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import re
import sys
import json
import psutil
import socket
import os
from os import path
import sqlite3
import platform
import requests
from os import path, times
import logging
import logging.config
from bs4 import BeautifulSoup
from urllib.request import urlopen
from clickhouse_driver import connect
from clickhouse_driver.errors import NetworkError
import re, os, sys, json, psutil, socket
from typing import Dict, List, AnyStr, Union
import logging
import logging.config

log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logging.ini')
logging.config.fileConfig(log_file_path)
from clickhouse_driver.errors import NetworkError
_log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logging.ini')
logging.config.fileConfig(_log_file_path)

class JsonSettings(object):
"""
Expand Down Expand Up @@ -298,16 +293,27 @@ def getHazStatus(self):
isHazelcast = bool(JsonSettings.parseJson("settings.json", "isHazelcast"))
hazInstances = JsonSettings.parseJson("settings.json", "instancesArray")
hazelcastPort = JsonSettings.parseJson("settings.json", "hazelcastPort")
print(f"isHazelcast: {isHazelcast}")
print(f"hazInstances: {hazInstances}")
full_haz_data= {}
if isHazelcast:
for host in hazInstances:
url = f"http://{host}:{hazelcastPort}/hazelcast/health"
haz = requests.request("GET", url)
print(f"Hazelcast status on instance [{url}]")
print(f"StatusCode: {haz.status_code}")
print(f"ResponseText: \n{haz.text}")

response = urlopen(url)
haz_data = json.loads(response.read())
node_haz_data = dict()
node_haz_data["nodeName"] = host
del haz_data['clusterSafe']
del haz_data['migrationQueueSize']
node_haz_data.update(haz_data)
full_haz_data[host] = node_haz_data
return full_haz_data
#print(f"Hazelcast status on instance [{url}]")
#print(f"StatusCode: {haz.status_code}")
#print(f"ResponseText: \n{haz.text}")
#values = [nodeState,clusterState,clusterSize]
#keys = ['nodeState','clusterState','clusterSize']
#haz_data[nodeName] = JsonSettings.fillDict(keys,values)
#haz_data = JsonSettings.fillDict(keys,values)
#return haz_data
#nodeState = haz[14:20]
except Exception as e:
pass
Expand Down Expand Up @@ -475,6 +481,25 @@ def insertSys(self):
logging.error(f'{self.cn} Exception: {e}', exc_info=1)
logging.error(f'{self.cn} SQL: {sql}')
logging.error(f'{self.cn} Data: {values}')

def insertHaz(self):
try:
haz = self.data.getHazStatus()
sql = """
INSERT INTO haz_info ('nodeName','nodeState','clusterState','clusterSize')
VALUES (?,?,?,?)
"""
values = self.getJsonValues(haz)
values = list()
for i in haz.values():
for j in i.values():
values.append(j)
values = tuple(values)
self.db.insertData(sql, values)
except Exception as e:
logging.error(f'{self.cn} Exception: {e}', exc_info=1)
logging.error(f'{self.cn} SQL: {sql}')
logging.error(f'{self.cn} Data: {values}')

def insertStats(self):
try:
Expand Down Expand Up @@ -579,5 +604,5 @@ def userKeys() -> List:
except Exception as e:
logging.error(f'{cn} Exception: {e}', exc_info=1)

d = DataCollector()
d.getHazStatus()
d = DbWorker()
d.insertHaz()
2 changes: 1 addition & 1 deletion logging.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ level=INFO
handlers=timedRotatingFileHandler

[formatter_timedRotatingFormatter]
format=[%(asctime)s] [%(levelname)s] [%(module)s] [%(message)s] [%(funcName)s]
format=[FStatsLogger %(asctime)s] [%(levelname)s] [%(module)s] [%(message)s] [%(funcName)s]
datefmt=%d-%m-%Y-%H-%M

[handler_timedRotatingFileHandler]
Expand Down
3 changes: 2 additions & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def _getMeta(self):

def _set(self, *args: AnyStr, table='stats') -> Dict:
"""
:param args* -> key to paste into SELECT statetment\n
:param args* -> key to paste into SELECT statetment
\n:param table -> table to use, default=stats
:return -> data dictionary\n
:e.g: _set("freeram,usedram,javamem,updated")
"""
Expand Down
Loading

0 comments on commit 8c90696

Please sign in to comment.