1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cn.wisenergy.web.config;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.jni.ArchNotSupportedException;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarLoader;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.io.*;
/**
* Created by m1991 on 2021/3/23 16:50
*/
@Slf4j
@Configuration
public class SigarConfig {
static {
try {
initSigar();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 初始化sigar的配置文件
*/
public static void initSigar() throws IOException {
SigarLoader loader = new SigarLoader(Sigar.class);
String lib = null;
try {
lib = loader.getLibraryName();
log.info("init sigar so文件====================="+lib);
} catch (ArchNotSupportedException var7) {
log.error("initSigar() error:{}",var7.getMessage());
}
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:/sigar.so/" + lib);
if (resource.exists()) {
InputStream is = resource.getInputStream();
File tempDir = new File("./log");
if (!tempDir.exists()){
tempDir.mkdirs();
}
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(new File(tempDir, lib), false));
int lentgh = 0;
while ((lentgh = is.read()) != -1){
os.write(lentgh);
}
is.close();
os.close();
System.setProperty("org.hyperic.sigar.path", tempDir.getCanonicalPath());
}
}
}