Commit 9ce3279a authored by Hardik Juneja's avatar Hardik Juneja Committed by Rafael Monnerat

slapos/promise: Add a promise to monitor memory

parent 6d596a4f
......@@ -70,6 +70,7 @@ setup(name=name,
'console_scripts': [
'agent = slapos.agent.agent:main',
'apache-mpm-watchdog = slapos.promise.apache_mpm_watchdog:main',
'check-computer-memory = slapos.promise.check_computer_memory:main',
'check-web-page-http-cache-hit = slapos.promise.check_web_page_http_cache_hit:main',
'check-feed-as-promise = slapos.checkfeedaspromise:main',
'check-error-on-apache-log = slapos.promise.check_error_on_apache_log:main',
......
#!/usr/bin/env python
"""
Check if memory usage is greater than given threshold.
Uses:
- /proc/meminfo
"""
import sys
import sqlite3
import argparse
import datetime
from slapos.collect.db import Database
def getFreeMemory(database, time, date):
mem = {}
database = Database(database)
try:
database.connect()
query_result = database.select("computer", date, "memory_size", limit=1)
result = zip(*query_result)
if not result or not result[0][0]:
print "couldn't fetch total memory, collectordb is empty?"
return 0
mem['total'] = result[0][0]
# fetch free and used memory
where_query = "time between '%s:00' and '%s:30' " % (time, time)
query_result = database.select("system", date, "memory_used, memory_free", where=where_query)
result = zip(*query_result)
if not result or not result[0][0]:
print "couldn't fetch free memory"
return 0
mem['free'] = result[0][0]
if not result or not result[1][0]:
print "couldn't fetch used memory"
return 0
mem['used'] = result[1][0]
finally:
database.close()
return mem
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-db", "--collectordb", required=True)
args = parser.parse_args()
# get last minute
partition = args.partition.replace('part', 'user')
now = datetime.datetime.now()
currentdate = now.strftime('%Y-%m-%d')
delta = datetime.timedelta(minutes=1)
currenttime = now - delta
currenttime = currenttime.time().strftime('%H:%M')
db_path = args.collectordb
if db_path.endswith("collector.db"):db_path=db_path[:-len("collector.db")]
memory = getFreeMemory(db_path, currenttime, currentdate)
threshold = float(memory['total']) * 0.2
if memory is 0:
return 0
if memory['free'] > threshold:
print "All Good. total memory: "+ str(memory['total']) + " and used memory: "+ str(memory['used'])
return 0
print "Ops! Memory is low, total memory: "+ str(memory['total']) + " and used memory: "+ str(memory['used'])
return 1
if __name__ == "__main__":
sys.exit(main())
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS computer (cpu_num_core real, cpu_frequency real, cpu_type text, memory_size real, memory_type text, partition_list text, date text, time text, reported integer NULL DEFAULT 0);
INSERT INTO "computer" VALUES(12.0,0.0,'0',33705312256.0,'0','/dev/md0=280665088;/dev/md1=19534663680;/dev/md2=463722799104','2017-09-15','00:00:04',1);
CREATE TABLE IF NOT EXISTS user (partition text, pid real, process text, cpu_percent real, cpu_time real, cpu_num_threads real, memory_percent real, memory_rss real, io_rw_counter real, io_cycles_counter real, date text, time text, reported integer NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS folder (partition text, disk_used real, date text, time text, reported integer NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS disk (partition text, used text, free text, mountpoint text, date text, time text, reported integer NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS temperature (sensor_id name, temperature real, alarm integer, date text, time text, reported integer NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS heating (model_id name, sensor_id name, initial_temperature real, final_temperature real, delta_time real, zero_emission_ratio real, date text, time text, reported integer NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS system (loadavg real, cpu_percent real, memory_used real, memory_free real, net_in_bytes real, net_in_errors real, net_in_dropped real, net_out_bytes real, net_out_errors real, net_out_dropped real, date text, time text, reported integer NULL DEFAULT 0);
INSERT INTO "system" VALUES(9.89,99.5,33139023872.0,566288384.0,2147589991472.0,0.0,0.0,1662685322551.0,0.0,90935.0,'2017-09-15','00:02:02',0);
COMMIT;
##############################################################################
#
# Copyright (c) 2017 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import unittest
import os
import sqlite3
from slapos.test.promise import data
from slapos.promise.check_computer_memory import getFreeMemory
class TestComputerMemory(unittest.TestCase):
def setUp(self):
self.base_path = "/".join(data.__file__.split("/")[:-1])
self.status = "ok"
self.db_file = '/tmp/collector.db'
# populate db
self.conn = sqlite3.connect(self.db_file)
f = open(self.base_path+"/memtest.sql")
sql = f.read()
self.conn.executescript(sql)
self.conn.close()
def test_check_memory(self):
self.assertEquals({'total': 33705312256.0, 'free': 33139023872.0, 'used': 566288384.0},
getFreeMemory('/tmp', '00:02', '2017-09-15'))
def test_check_memory_with_unavailable_dates(self):
self.assertEquals(0, getFreeMemory('/tmp', '18:00', '2017-09-14'))
def tearDown(self):
if os.path.exists(self.db_file):
os.remove(self.db_file)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment