|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.alipay.lookout.os.linux; |
| 18 | + |
| 19 | +import com.alipay.lookout.api.Gauge; |
| 20 | +import com.alipay.lookout.api.Id; |
| 21 | +import com.alipay.lookout.api.Registry; |
| 22 | +import com.alipay.lookout.api.composite.MixinMetric; |
| 23 | +import com.alipay.lookout.common.log.LookoutLoggerFactory; |
| 24 | +import com.alipay.lookout.os.CachedMetricsImporter; |
| 25 | +import com.alipay.lookout.os.utils.FileUtils; |
| 26 | +import org.slf4j.Logger; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +/** |
| 33 | + * resolve memory metrics from "/proc/meminfo" |
| 34 | + * |
| 35 | + |
| 36 | + * 2019-03-12 17:30 |
| 37 | + **/ |
| 38 | +public class MemoryStatsMetricsImporter extends CachedMetricsImporter { |
| 39 | + |
| 40 | + private final Logger logger = LookoutLoggerFactory |
| 41 | + .getLogger(DiskUsageMetricsImporter.class); |
| 42 | + |
| 43 | + private static final String DEFAULT_FILE_PATH = "/proc/meminfo"; |
| 44 | + private static final String SPLIT = "\\s+"; |
| 45 | + private String filePath; |
| 46 | + private Memstats memstats; |
| 47 | + |
| 48 | + /** |
| 49 | + * default constructor |
| 50 | + */ |
| 51 | + public MemoryStatsMetricsImporter() { |
| 52 | + this(DEFAULT_FILE_PATH, DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param filePath filePath |
| 57 | + * @param timeout timeout |
| 58 | + * @param timeoutUnit timeoutUnit |
| 59 | + */ |
| 60 | + public MemoryStatsMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) { |
| 61 | + super(timeout, timeoutUnit); |
| 62 | + this.filePath = filePath; |
| 63 | + memstats = new Memstats(); |
| 64 | + disable = !new File(filePath).exists(); |
| 65 | + if (!disable) |
| 66 | + loadIfNessesary(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void doRegister(Registry registry) { |
| 71 | + Id id = registry.createId("os.memory.stats"); |
| 72 | + MixinMetric mixin = registry.mixinMetric(id); |
| 73 | + |
| 74 | + mixin.gauge("total.bytes", new Gauge<Long>() { |
| 75 | + @Override |
| 76 | + public Long value() { |
| 77 | + loadIfNessesary(); |
| 78 | + return memstats.memTotal; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + mixin.gauge("free.bytes", new Gauge<Long>() { |
| 83 | + @Override |
| 84 | + public Long value() { |
| 85 | + loadIfNessesary(); |
| 86 | + return memstats.memFree; |
| 87 | + } |
| 88 | + }); |
| 89 | + mixin.gauge("buffers.bytes", new Gauge<Long>() { |
| 90 | + @Override |
| 91 | + public Long value() { |
| 92 | + loadIfNessesary(); |
| 93 | + return memstats.buffers; |
| 94 | + } |
| 95 | + }); |
| 96 | + mixin.gauge("cached.bytes", new Gauge<Long>() { |
| 97 | + @Override |
| 98 | + public Long value() { |
| 99 | + loadIfNessesary(); |
| 100 | + return memstats.cached; |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected void loadValues() { |
| 108 | + try { |
| 109 | + List<String> lines = FileUtils.readFileAsStringArray(filePath); |
| 110 | + Memstats ms = new Memstats(); |
| 111 | + int i = 0; |
| 112 | + for (String line : lines) { |
| 113 | + if (i == 4) |
| 114 | + break; |
| 115 | + String[] infos = line.split(SPLIT); |
| 116 | + if (infos == null) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + if ("MemTotal:".equals(infos[0])) { |
| 120 | + ms.memTotal = Long.parseLong(infos[1]) * 1024; |
| 121 | + i++; |
| 122 | + continue; |
| 123 | + } |
| 124 | + if ("MemFree:".equals(infos[0])) { |
| 125 | + ms.memFree = Long.parseLong(infos[1]) * 1024; |
| 126 | + i++; |
| 127 | + continue; |
| 128 | + } |
| 129 | + if ("Buffers:".equals(infos[0])) { |
| 130 | + ms.buffers = Long.parseLong(infos[1]) * 1024; |
| 131 | + i++; |
| 132 | + continue; |
| 133 | + } |
| 134 | + if ("Cached:".equals(infos[0])) { |
| 135 | + ms.cached = Long.parseLong(infos[1]) * 1024; |
| 136 | + i++; |
| 137 | + continue; |
| 138 | + } |
| 139 | + } |
| 140 | + memstats = ms; |
| 141 | + } catch (Exception e) { |
| 142 | + logger.debug("warning,can't parse line at /proc/meminfo", e.getMessage()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private class Memstats { |
| 147 | + long memTotal; |
| 148 | + long memFree; |
| 149 | + long cached; |
| 150 | + long buffers; |
| 151 | + } |
| 152 | +} |
0 commit comments