mam problem z takimi dwoma klasami:
import java.io.IOException;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SSHUtils {
private String host="192.168.8.106";
private String user="xgen";
private String password="asdzxcasdzxc123";
JSch jsch = new JSch();
Session session;
Channel channel;
InputStream in;
java.util.Properties config;
public String exec(String command1) throws IOException, JSchException {
try{
config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
return new String(tmp, 0, i);
}
if(channel.isClosed()){
//System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
return e.getMessage();
}
return "ERROR";
}
}
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import com.jcraft.jsch.JSchException;
import com.mysql.jdbc.StringUtils;
public class sendInfoTask {
static ConnectionUtils connection = new ConnectionUtils();
static Sigar sigar = new Sigar();
static SSHUtils ssh = new SSHUtils();
static String sshResult = "";
static Mem mem = null;
static CpuPerc cpuperc = null;
static FileSystemUsage filesystemusage = null;
static String query = "";
static List<String> servers = new ArrayList<String>();
static ResultSet rs;
static List<String> serversOnline = new ArrayList<String>();
static String name = "";
static int sTrue = 0;
public static void startInterval() throws InterruptedException {
while(true) {
connection.openConnection();
try {
mem = sigar.getMem();
cpuperc = sigar.getCpuPerc();
filesystemusage = sigar.getFileSystemUsage("/");
} catch (SigarException se) {
se.printStackTrace();
}
long RAM = Math.round(mem.getUsedPercent());
long CPU = Math.round((cpuperc.getCombined()*100));
long HDD = Integer.valueOf((int) (filesystemusage.getUsePercent()*100));
query = "UPDATE ServerInfo SET RAM="+RAM+",CPU="+CPU+",HDD="+HDD+" WHERE ServerName=\"XgenVPS\"";
connection.sendQuery(query);
query = "SELECT * FROM Servers";
rs = connection.sendQuery(query);
try {
while(rs.next()) {
servers.add(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
sshResult = ssh.exec("screen -list");
} catch (IOException | JSchException e) {
e.printStackTrace();
}
if(sshResult.contains("No Sockets found")){
query = "UPDATE Servers SET Status=\"OFFLINE\"";
connection.sendQuery(query);
}else {
//System.out.println(sshResult);
for(int i = 0; i<sshResult.length(); i++) {
if(StringUtils.isStrictlyNumeric(String.valueOf(sshResult.charAt(i)))) {
sTrue++;
name = name + String.valueOf(sshResult.charAt(i));
if(sTrue == 5) {
if(String.valueOf(sshResult.charAt(i+1)).equals(".")) {
serversOnline.add(name);
//System.out.println("<--->" + name + "<--->");
sTrue = 0;
name = "";
}else {
//System.out.println(">----"+name+"----<");
sTrue = 0;
name = "";
}
}
}else {
sTrue = 0;
name = "";
}
}
boolean eq = false;
for(String server : servers){
for(String serverOnline : serversOnline) {
if(server.equalsIgnoreCase(serverOnline)) {
connection.sendQuery("UPDATE Servers SET Status=\"ONLINE\" WHERE Name=\""+server+"\"");
eq = true;
break;
}
}
if(!eq) {
connection.sendQuery("UPDATE Servers SET Status=\"OFFLINE\" WHERE Name=\""+server+"\"");
}else {
eq = false;
}
}
}
connection.closeConnection();
System.out.println("informacje sostały pomyśnie wysłane do bazy danych");
Thread.sleep(2000);
}
}
}
otóż generalnie wszystko dziłą bez zarzutu, ale gdy sprawdzam sobie na bieżąco w bazie co mi daje Sigar, to widze systematyczne zwiększanie zużycia RAMU przez ten program. Więc jak teraz ogarnąć to zużycie? Jakoś inaczej poukładać zmienne? zedytować inaczej klase SSH? prosiłbym o konkrety :). dodam że program tylko uruchamia sendInfoTask().startInterval();
Dodam że z casem to wysyłanie stało się coraz bardziej opóżnione