1 /*
2  * Copyright (C) 2021 Jakub Kruszona-Zawadzki, Core Technology Sp. z o.o.
3  *
4  * This file is part of MooseFS.
5  *
6  * MooseFS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, version 2 (only).
9  *
10  * MooseFS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with MooseFS; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
18  * or visit http://www.gnu.org/licenses/gpl-2.0.html
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <time.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <syslog.h>
32 #include <errno.h>
33 
34 #include "charts.h"
35 #include "main.h"
36 
37 #include "bgjobs.h"
38 #include "csserv.h"
39 #include "mainserv.h"
40 #include "masterconn.h"
41 #include "hddspacemgr.h"
42 #include "replicator.h"
43 
44 #include "cpuusage.h"
45 #include "memusage.h"
46 
47 #include "chartsdefs.h"
48 
49 static const uint32_t calcdefs[]=CALCDEFS
50 static const statdef statdefs[]=STATDEFS
51 static const estatdef estatdefs[]=ESTATDEFS
52 
chartsdata_refresh(void)53 void chartsdata_refresh(void) {
54 	uint64_t data[CHARTS];
55 	uint64_t bin,bout;
56 	uint32_t i,opr,opw,dbr,dbw,dopr,dopw,movl,movh,repl;
57 	uint32_t op_cr,op_de,op_ve,op_du,op_tr,op_dt,op_te;
58 	uint32_t jobs;
59 	uint64_t scpu,ucpu;
60 	uint64_t rss,virt;
61 
62 	for (i=0 ; i<CHARTS ; i++) {
63 		data[i]=CHARTS_NODATA;
64 	}
65 
66 	cpu_used(&scpu,&ucpu);
67 
68 	if (scpu>0 || ucpu>0) {
69 		data[CHARTS_UCPU] = (ucpu*6)/100;
70 		data[CHARTS_SCPU] = (scpu*6)/100;
71 	}
72 
73 	if (mem_used(&rss,&virt)) {
74 		data[CHARTS_MEMORY_RSS] = rss;
75 		data[CHARTS_MEMORY_VIRT] = virt;
76 	}
77 
78 	masterconn_stats(data+CHARTS_MASTERIN,data+CHARTS_MASTEROUT);
79 	job_stats(&jobs);
80 	data[CHARTS_LOAD]=jobs;
81 	csserv_stats(data+CHARTS_CSSERVIN,data+CHARTS_CSSERVOUT);
82 	mainserv_stats(&bin,&bout,&opr,&opw);
83 	data[CHARTS_CSSERVIN]+=bin;
84 	data[CHARTS_CSSERVOUT]+=bout;
85 	data[CHARTS_HLOPR]=opr;
86 	data[CHARTS_HLOPW]=opw;
87 	hdd_stats(&bin,&bout,&opr,&opw,&dbr,&dbw,&dopr,&dopw,&movl,&movh,data+CHARTS_RTIME,data+CHARTS_WTIME);
88 	data[CHARTS_HDRBYTESR]=bin;
89 	data[CHARTS_HDRBYTESW]=bout;
90 	data[CHARTS_HDRLLOPR]=opr;
91 	data[CHARTS_HDRLLOPW]=opw;
92 	data[CHARTS_DATABYTESR]=dbr;
93 	data[CHARTS_DATABYTESW]=dbw;
94 	data[CHARTS_DATALLOPR]=dopr;
95 	data[CHARTS_DATALLOPW]=dopw;
96 	data[CHARTS_MOVELS]=movl;
97 	data[CHARTS_MOVEHS]=movh;
98 	replicator_stats(data+CHARTS_CSREPIN,data+CHARTS_CSREPOUT,&repl);
99 	data[CHARTS_REPL]=repl;
100 	hdd_op_stats(&op_cr,&op_de,&op_ve,&op_du,&op_tr,&op_dt,&op_te);
101 	data[CHARTS_CREATE]=op_cr;
102 	data[CHARTS_DELETE]=op_de;
103 	data[CHARTS_VERSION]=op_ve;
104 	data[CHARTS_DUPLICATE]=op_du;
105 	data[CHARTS_TRUNCATE]=op_tr;
106 	data[CHARTS_DUPTRUNC]=op_dt;
107 	data[CHARTS_TEST]=op_te;
108 	data[CHARTS_CHANGE]=op_ve+op_du+op_tr+op_dt;
109 
110 	charts_add(data,main_time()-60);
111 }
112 
chartsdata_term(void)113 void chartsdata_term(void) {
114 	chartsdata_refresh();
115 	charts_store();
116 	charts_term();
117 }
118 
chartsdata_store(void)119 void chartsdata_store(void) {
120 	charts_store();
121 }
122 
chartsdata_init(void)123 int chartsdata_init (void) {
124 	cpu_init();
125 
126 	main_time_register(60,0,chartsdata_refresh);
127 	main_time_register(3600,30,chartsdata_store);
128 	main_destruct_register(chartsdata_term);
129 	return charts_init(calcdefs,statdefs,estatdefs,CHARTS_FILENAME,0);
130 }
131