ÃÑ ÆäÀÌÁö ¼ö : 3224
![]()
|
Facebook Joinc ±×·ì
Joinc QA »çÀÌÆ®
![]()
Tweet
joinc´Â Firefox¿Í chrome¿¡¼ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù. 1 ¼Ò°³
ÀÌ ¹®¼´Â ¸®´ª½º ¿î¿µÃ¼Á¦¸¦ ±âÁØÀ¸·Î ÀÛ¼ºµÉ °ÍÀÌ´Ù.
¸®´ª½º ¿î¿µÃ¼Á¦¿¡¼ ½Ã½ºÅÛÁ¤º¸¸¦ ¾ò´Â ¹æ¹ýÀº ¾Æ·¡ÀÇ ¹®¼¸¦ ÅëÇØ¼ ¿©·¯¹ø ¼³¸íµÇ¾ú´Ù. ¾Æ·¡ÀÇ proc ÆÄÀϽýºÅÛ ºÐ¼®°ü·Ã ¹®¼¸¦ Àоú´Ù¸é, ¾î·ÆÁö ¾Ê°Ô CPU »ç¿ëÀ²À» ÃøÁ¤ÇÏ´Â ÇÁ·Î±×·¥À» ¸¸µé ¼ö ÀÖÀ» °ÍÀÌ´Ù.
2 ¸ðµâÈ
ÀÌ ÇÁ·Î±×·¥Àº È®À强À» °í·ÁÇÑ Agent, Manager ½Ã½ºÅÛ¿¡¼ ¼Ò°³ÇÑ ¹æ½ÄÀ¸·Î ÀÛ¼ºµÉ °ÍÀÌ´Ù. Áï
º¹ÀâÇÏÁö ¾ÊÀº ÄÚµåÀÌ´Ï ÁÖ¼®À¸·Î ¼³¸íÀ» ´ë½ÅÇϰڴÙ. // Standard C++ Library #include <iostream> // Common Library #include <cinterface.h> // Standard C Library #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <ctype.h> #define K4 0 #define K6 1 using namespace std; int isTotal=0; int findCpu=0; int GetCpuNum(); int Type = 0; struct _CpuStat { unsigned int User; unsigned int System; unsigned int Idle; }; struct _CpuStat CpuStat[2]; char *rtvstr; int Init() { GetCpuNum(); rtvstr = (char *)malloc(80); return 1; } int cidx = 0; // CPU °¹¼ö¸¦ ¾ò¾î¿Â´Ù. int GetCpuNum() { FILE *fp; fp = fopen("/proc/stat", "r"); char buf[80]; isTotal=0; findCpu=0; if (fp == NULL) { return 0; } while(fgets(buf, 80, fp)) { if (strncmp(buf, "cpu", 3) != 0) { continue; } if (isdigit(buf[3])) { findCpu++; } else { isTotal = 1; } } fclose(fp); return 1; } // Cpu °¹¼ö¸¦ ¸®ÅÏÇÑ´Ù. int NumRows() { return findCpu; } // proc ÆÄÀϽýºÅÛÀ» ºÐ¼®Çؼ Cpu »ç¿ëÀ² Á¤º¸¸¦ ¾ò¾î¿Â´Ù. // /proc/stat ÀÇ »ç¿ëÀ² ÃÑÇÕ¶óÀθ¸ Àоî¿Ã °ÍÀÌ´Ù. char *Read() { FILE *fp; char buf[80]; fp = fopen("/proc/stat", "r"); char cpuid[8]; int nused; if (fp == NULL) { return NULL; } while(fgets(buf, 80, fp)) { if (!strncmp(buf, "cpu", 3)) { sscanf(buf, "%s %d %d %d %d", cpuid, &CpuStat[cidx%2].User, &nused, &CpuStat[cidx%2].System, &CpuStat[cidx%2].Idle ); break; } } // óÀ½½ÇÇàÇß´Ù¸é, 1Ãʸ¦ ½®´ÙÀ½ Àç±ÍÈ£Ãâ ÇÑ´Ù. if (!cidx) { sleep(1); cidx++; *Read(); } cidx++; int diff1; int diff2; int diff3; int Idle, System, User; diff1 = CpuStat[(cidx+1)%2].User - CpuStat[(cidx)%2].User; diff2 = CpuStat[(cidx+1)%2].System - CpuStat[(cidx)%2].System; diff3 = CpuStat[(cidx+1)%2].Idle - CpuStat[(cidx)%2].Idle; Idle = (diff3*100)/(diff1+diff2+diff3); System = (diff2*100)/(diff1+diff2+diff3); User = (diff1*100)/(diff1+diff2+diff3); sprintf(rtvstr,"CPU=%d,%d,%d\n", User,System,Idle); return rtvstr; } int Clean() { if (rtvstr != NULL) free(rtvstr); rtvstr = NULL; return 1; }
ÃÖÁ¾¸ñÀûÀº PlugIn ÇüÅ·ΠAgent¿¡¼ ·ÎµùÇϰí, ±× °á°ú¸¦ Manager¿¡°Ô º¸³»´Â °ÍÀ̰ÚÁö¸¸, ±×°Ç ³ªÁßÀÏÀ̰í ÀÏ´Ü main ÇÔ¼ö¸¦ Æ÷ÇÔÇÑ °£´ÜÇÑ Äڵ带 ¸¸µé¾î¼ Å×½ºÆ®¸¦ ÇØº¸±â·Î Çß´Ù. ¾ò¾î³½ Cpu Á¤º¸´Â RRD ÇüÅ·ΠÀúÀåÇØ¼, ±×·¡ÇÁ·Î Ãâ·ÂÇÒ °ÍÀÌ´Ù. À̸¦ À§Çؼ ´ÙÀ½°ú °°ÀÌ RRD µ¥ÀÌÅͺ£À̽º¸¦ »ý¼ºÇß´Ù. # rrdtool create cpuusage.rrd --start 1185174483 --step 300 \ DS:user:GAUGE:600:U:U \ DS:system:GAUGE:600:U:U \ DS:usage:GAUGE:600:U:U \ RRA:AVERAGE:0.5:1:600 \ RRA:AVERAGE:0.5:6:700 \ RRA:AVERAGE:0.5:24:775 \ RRA:AVERAGE:0.5:288:797
´ÙÀ½Àº C ÄÚµå´Ù. RRD°¡ ¼³Ä¡µÇ¾î ÀÖ´Ù¸é, RRD¿¡¼ Á¦°øÇÏ´Â C API¸¦ ÀÌ¿ëÇØ¼ CPU »ç¿ëÀ² µ¥ÀÌÅ͸¦ Update ÇÒ ¼öµµ ÀÖ°ÚÁö¸¸, °£´ÜÇÏ°Ô system() ÇÔ¼ö¸¦ ÀÌ¿ëÇØ¼ Update ½Ã۵µ·Ï Çß´Ù. #include <qoscpu.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
// Read ·Î Àоî¿Â, Cpu Usage Á¤º¸´Â ¹è¿¿¡ ÀúÀåÇÑ´Ù.
// ÀúÀåÀº 5ÃÊ´ÜÀ§·Î 5ºÐµ¿¾ÈÀ̸ç, ¹è¿ÀÇ Å©±â´Â 60ÀÌ µÈ´Ù.
// ÀúÀåµÈ °ªÀº 5ºÐ´ÜÀ§·Î Æò±ÕÀ» ³»±â À§Çؼ »ç¿ëµÈ´Ù.
class CircleQueue
{
private:
int User[60];
int System[60];
int Idle[60];
int idx;
int size;
public:
CircleQueue()
{
idx = 0;
size = 0;
memset((void *)User, 0x00, sizeof(int)*60);
memset((void *)System, 0x00, sizeof(int)*60);
memset((void *)Idle, 0x00, sizeof(int)*60);
}
void Push(int aUser, int aSystem, int aIdle)
{
User[idx] = aUser;
System[idx] = aSystem;
Idle[idx] = aIdle;
idx++;
if (idx == 60)
idx = 0;
size++;
}
int Length()
{
if (size > 59)
return 60;
else
return size;
}
int GetSystem(int index)
{
return System[index];
}
int GetUser(int index)
{
return User[index];
}
int GetIdle(int index)
{
return Idle[index];
}
};
// Daemon ÇÁ·Î¼¼½º·Î ¸¸µç´Ù.
int MakeDaemon()
{
pid_t pid;
if ((pid = fork()) < 0)
{
exit(0);
}
else if (pid != 0)
exit(0);
setsid();
return 1;
}
int main()
{
char *str;
char *sp;
CircleQueue *Cqueue;
int sum;
int User, System, Idle;
Cqueue = new CircleQueue();
int idx = 1;
char command[256];
MakeDaemon();
if (!Init())
perror("Error");
while(1)
{
str = Read();
sp = strstr(str, "=");
User = atoi(sp+1);
sp = strstr(sp+1, ",");
System = atoi(sp+1);
sp = strstr(sp+1, ",");
Idle = atoi(sp+1);
Cqueue->Push(User, System, Idle);
sleep(5);
idx++;
User = 0;
System = 0;
Idle = 0;
if (!(idx % 60))
{
printf("Size %d\n", Cqueue->Length());
sum = 0;
for(int i = 0; i < Cqueue->Length(); i++)
{
User += Cqueue->GetUser(i);
System += Cqueue->GetSystem(i);
}
Idle = User+System;
sprintf(command, "rrdtool update /rrd/cpuusage.rrd %d:%d:%d:%d",
time((time_t *)NULL),
User/Cqueue->Length(),
System/Cqueue->Length(), Idle);
system(command);
}
}
return 0;
}
3 RRD¸¦ ÀÌ¿ëÇÑ µ¥ÀÌÅÍ °ü¸®
ÀÌÁ¦ RRD¸¦ ÀÌ¿ëÇØ¼, ÀúÀåµÈ µ¥ÀÌÅÍ·Î ºÎÅÍ ±×·¡ÇÁ¸¦ ¸¸µé¾î º¸µµ·Ï ÇÏÀÚ. ±×·¡ÇÁ¸¦ À§Çؼ ´ÙÀ½°ú °°Àº °£´ÜÇÑ ½ºÅ©¸³Æ®¸¦ ¸¸µé¾ú´Ù. 6½Ã°£°ú 24½Ã°£ ±×·¡ÇÁ¸¦ ¸¸µéµµ·Ï Çß´Ù. ¾à°£ ¼öÁ¤Çϸé, ÁÖ°£/¿ù°£/³â°£ µ¥ÀÌÅ͵µ ¸¸µé ¼ö ÀÖÀ» °ÍÀÌ´Ù. #!/bin/sh CUTIME=`date +%s` # 6 hour FROM=`echo "$CUTIME - (3600*6)" | bc` rrdtool graph cpuusage.png \ -s $FROM --vertical-label Usage \ DEF:linea=cpuusage.rrd:user:AVERAGE \ STACK:linea#0000FF:"user" \ "GPRINT:linea:AVERAGE:%3.2lf Avg" \ "GPRINT:linea:MAX:%3.2lf MAX" \ "GPRINT:linea:MIN:%3.2lf MIN\n" \ DEF:lineb=cpuusage.rrd:system:AVERAGE \ STACK:lineb#00FF00:"system" \ "GPRINT:lineb:AVERAGE:%3.2lf Avg" \ "GPRINT:lineb:MAX:%3.2lf MAX" \ "GPRINT:lineb:MIN:%3.2lf MIN\n" \ HRULE:100000#FF0000:"Maximum allowed" # 24 hour FROM=`echo "$CUTIME - (3600*24)" | bc` rrdtool graph cpuusage24h.png \ -s $FROM --vertical-label Usage \ DEF:linea=cpuusage.rrd:user:AVERAGE \ STACK:linea#0000FF:"user" \ "GPRINT:linea:AVERAGE:%3.2lf Avg" \ "GPRINT:linea:MAX:%3.2lf MAX" \ "GPRINT:linea:MIN:%3.2lf MIN\n" \ DEF:lineb=cpuusage.rrd:system:AVERAGE \ STACK:lineb#00FF00:"system" \ "GPRINT:lineb:AVERAGE:%3.2lf Avg" \ "GPRINT:lineb:MAX:%3.2lf MAX" \ "GPRINT:lineb:MIN:%3.2lf MIN\n" \ HRULE:100000#FF0000:"Maximum allowed" |
|
||||||||||||||||||||||||||||||||||||
|
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù. |
|