1.需求场景
利用java程序监控Linux服务器内存变化,根据设定的内存阈值发报警邮件
2.项目环境
普通java项目,jar形式
3.解决方法
(1)读取Linux 内存监控文件(/proc/meminfo),获取内存信息。
public static int [] getLinuxMemInfo() throws IOException, InterruptedException
{
File file = new File("/proc/meminfo" );
BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(file)));
int [] result = new int [ 4 ];
String str = null ;
StringTokenizer token = null ;
while ((str = br.readLine()) != null )
{
token = new StringTokenizer(str);
if (!token.hasMoreTokens())
continue ;
str = token.nextToken();
if (!token.hasMoreTokens())
continue ;
if (str.equalsIgnoreCase("MemTotal:" ))
result[0 ] = Integer.parseInt(token.nextToken(