📄 mmemorymonitor.java
字号:
package net.jumperz.app.MGuardian.plugin;
import java.awt.Color;
import java.io.*;
import java.util.*;
import net.jumperz.net.*;
import net.jumperz.app.MGuardian.*;
import net.jumperz.app.MGuardian.plugin.*;
import net.jumperz.ext.org.jrobin.core.*;
import net.jumperz.ext.org.jrobin.graph.*;
import net.jumperz.io.*;
import net.jumperz.util.*;
public class MMemoryMonitor
extends MGuardianPlugin
implements MObserver1, MCommand
{
private int time;
private MThreadPool threadPool;
private String rrdFileName;
private String graphFileName;
private RrdDb rrdDb;
private static final int INTERVAL = 300; // 5 minutes
private static final int LINE_WIDTH = 2;
private static final String GRAPH_SOURCE_NAME = "used memory";
private static final String DATA_SOURCE_NAME = "used_memory";
//--------------------------------------------------------------------------------
public void update()
{
++time;
if( time == INTERVAL )
{
threadPool.addCommand( this );
time = 0;
}
}
//--------------------------------------------------------------------------------
public void startup()
throws IOException
{
System.setProperty( "java.awt.headless", "true" );
try
{
initRrdFile();
}
catch( RrdException e )
{
throw new IOException( e.getMessage() );
}
registToTimer();
}
//--------------------------------------------------------------------------------
private void initRrdFile()
throws IOException, RrdException
{
rrdFileName = getRequiredProperty( "memoryMonitor.rrdFileName" );
File rrdFile = new File( rrdFileName );
if( !rrdFile.exists() )
{
createNewRrdFile();
}
else
{
rrdDb = new RrdDb( rrdFileName );
}
graphFileName = getRequiredProperty( "memoryMonitor.graphFileName" );
}
//--------------------------------------------------------------------------------
private void createNewRrdFile()
throws IOException, RrdException
{
RrdDef rrdDef = new RrdDef( rrdFileName );
rrdDef.setStartTime( ( System.currentTimeMillis() ) / 1000 ); // now
rrdDef.addDatasource( DATA_SOURCE_NAME, "GAUGE", 600, 0, Double.NaN );
rrdDef.addArchive( "AVERAGE", 0.5, 1, 600 );
rrdDef.addArchive( "AVERAGE", 0.5, 6, 700 );
rrdDef.addArchive( "AVERAGE", 0.5, 24, 775 );
rrdDef.addArchive( "AVERAGE", 0.5, 288, 797 );
rrdDef.addArchive( "MAX", 0.5, 1, 600 );
rrdDef.addArchive( "MAX", 0.5, 6, 700 );
rrdDef.addArchive( "MAX", 0.5, 24, 775 );
rrdDef.addArchive( "MAX", 0.5, 288, 797 );
rrdDb = new RrdDb( rrdDef );
}
//--------------------------------------------------------------------------------
private void registToTimer()
{
threadPool = MGuardianImpl.getInstance().getThreadPool();
MTimer timer = MGuardianImpl.getInstance().getTimer();
timer.register1( this );
}
//--------------------------------------------------------------------------------
public void execute()
{
long freeMemory = Runtime.getRuntime().freeMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
long usedMemory = totalMemory - freeMemory;
String usedMemoryStr = Long.toString( usedMemory );
try
{
//update
Sample sample = rrdDb.createSample();
sample.setAndUpdate( "N:" + usedMemoryStr );
//create graph
RrdGraphDef rrdGraphDef = new RrdGraphDef();
long end = System.currentTimeMillis() / 1000;
long start = end - 20400;
// long start = end - ( 81600 * 1000 );
rrdGraphDef.setVerticalLabel( GRAPH_SOURCE_NAME );
rrdGraphDef.setTimePeriod( start, end );
rrdGraphDef.datasource( GRAPH_SOURCE_NAME, rrdFileName, DATA_SOURCE_NAME, "AVERAGE" );
rrdGraphDef.line( GRAPH_SOURCE_NAME, new Color( 0xFF, 0, 0 ), null, LINE_WIDTH );
RrdGraph rrdGraph = new RrdGraph( rrdGraphDef );
// save to file
if( graphFileName.toLowerCase().indexOf( ".gif" ) > -1 )
{
rrdGraph.saveAsGIF( graphFileName );
}
else
{
rrdGraph.saveAsPNG( graphFileName );
}
}
catch( Exception e )
{
e.printStackTrace();
}
}
//--------------------------------------------------------------------------------
public void shutdown()
{
try
{
rrdDb.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
//--------------------------------------------------------------------------------
public void breakCommand()
{
}
//--------------------------------------------------------------------------------
public Map execute( Map sessionInfo )
{
return null;
}
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -