"proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。"
这里将介绍如何从/proc文件系统中获取与防火墙相关的一些性能参数,以及如何通过/proc文件系统修改内核的相关配置。
1、从/proc文件系统获取相关的性能参数
cpu使用率:/proc/stat
内存使用情况: /proc/meminfo
网络负载信息:/proc/net/dev
相应的计算方法:
(1) 处理器使用率
(2) 内存使用率
(3) 流入流出数据包
(4) 整体网络负载
这些数据分别要从/proc/stat、/proc/net/dev、/proc/meminfo三个文件中提取。
(1) 处理器使用率
这里要从/proc/stat中提取四个数据:用户模式(user)、低优先级的用户模式(nice)、内核模式(system)以及空闲的处理器时间(idle)。它们均位于/proc/stat文件的第一行。
这个是通过cat命令获取的Android的CUP信息。
以上数据对应的意义为:
(User)5215 (nice)170 (System)3640 (Idle)5001 (iowait)63 (Irq)5 (sotfirq)18 0 0
CPU利用率 = 100 *(user + nice + system)/(user + nice + system + idle)
(2) 内存使用率
这里需要从/proc/meminfo文件中提取两个数据,当前内存的使用量(cmem)以及内存总量(amem)。
内存使用百分比 = 100 * (cmem / umem)
(3) 网络利用率
为了得到网络利用率的相关数据,需要从/proc/net/dev文件中获得两个数据:从本机输出的数据包数,流入本机的数据包数。它们都位于这个文件的第四行。
性能收集程序开始记录下这两个数据的初始值,以后每次获得这个值后均减去这个初始值即为从集群启动开始从本节点通过的数据包。
利用上述数据计算出网络的平均负载,方法如下:
平均网络负载 = (输出的数据包+流入的数据包) / 2
2. 通过/proc文件系统调整相关的内核配置
允许ip转发 /proc/sys/net/ipv4/ip_forward
禁止ping/proc/sys/net/ipv4/icmp_echo_ignore_all
可以在命令行下直接往上述两个“文件”里头写入"1"来实现相关配置,如果写入"0"将取消相关配置。不过在系统重启以后,这些配置将恢复默认设置,所以,如果想让这些修改生效,可以把下面的配置直接写入/etc/profile文件,或者其他随系统启动而执行的程序文件中。
1. echo 1 > /proc/sys/net/ipv4/ip_forward
2. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
如果需要获取其他的性能参数,或者需要对内核进行更多的配置,可以参考proc文件系统介绍,也可以直接通过man proc查看相关的信息。
这些也同样适合Android系统,如果要获取Android系统的Cpu、内存、网络的利用情况,就可以通过读取proc文件系统中的文件。
网络的数据格式
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 31456 705 0 0 0 0 0 0 31456 705 0 0 0 0 0 0
usb0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
tunl0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
gre0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
svnet0: 6016 290 0 0 0 0 0 0 2129 130 0 0 0 0 0 0
eth0: 177509 677 0 0 0 0 0 0 33802 291 0 0 0 0 0 0
android手机流量信息系统是LINUX内核 记录在/proc/self/net/dev文件里面
我们可以看看dev文件的格式
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
D:/Program Files/Java/sdk/android-sdk-windows/tools>adb shell
# cd proc
cd proc
# cd net
cd net
# cat dev
cat dev
Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packe ts errs drop fifo colls carrier compressed
lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
eth0: 7069733 86239 0 0 0 0 0 0 12512463 741 79 0 0 0 0 0 0
tunl0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
gre0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #
我们要获取流量可以通过这个文件进行读取
我读取的源代码如下
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Calendar;
- import org.apache.http.util.EncodingUtils;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.IBinder;
- import android.widget.Toast;
-
-
-
- public class mService1 extends Service
- {
- private Handler objHandler = new Handler();
- private int intCounter = 0;
- private int mHour;
- private int mMinute;
- private int mYear;
- private int mMonth;
- private int mDay;
- private String mdate;
-
- final public String DEV_FILE = "/proc/self/net/dev";
- String[] ethdata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0", "0", "0", "0", "0" };
- String[] gprsdata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0", "0", "0", "0", "0", "0" };
- String[] wifidata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0", "0", "0", "0", "0", "0" };
- String data = "0,0,0,0,0,0,0,0,0,0,0,0";
-
- final String ETHLINE = " eth0";
- final String GPRSLINE = "rmnet0";
-
- final String WIFILINE = " wlan0";
- final String TEXT_ENCODING = "UTF-8";
- final public String ONPATH = "/data/data/zy.dnh/on.txt";
- final public String LOGPATH = "/data/data/zy.dnh/log.txt";
-
- private Runnable mTasks = new Runnable()
- {
- public void run()
- {
- refresh();
- intCounter++;
-
- objHandler.postDelayed(mTasks, 3000);
- }
- };
-
- @Override
- public void onStart(Intent intent, int startId)
- {
-
- objHandler.postDelayed(mTasks, 0);
- super.onStart(intent, startId);
- }
-
- @Override
- public void onCreate()
- {
-
- super.onCreate();
- }
-
- @Override
- public IBinder onBind(Intent intent)
- {
-
- return null;
- }
-
- @Override
- public void onDestroy()
- {
-
-
- objHandler.removeCallbacks(mTasks);
- super.onDestroy();
- }
-
- public void DisplayToast(String str)
- {
- Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
- }
-
- public void readdev()
- {
- FileReader fstream = null;
- try {
- fstream = new FileReader(DEV_FILE);
- }
- catch (FileNotFoundException e) {
- DisplayToast("Could not read " + DEV_FILE);
- }
-
- BufferedReader in = new BufferedReader(fstream, 500);
- String line;
- String[] segs;
- String[] netdata;
- int count = 0;
- int k;
- int j;
- try {
- while ((line = in.readLine()) != null) {
- segs = line.trim().split(":");
- if (line.startsWith(ETHLINE))
- {
- netdata = segs[1].trim().split(" ");
- for (k = 0, j = 0; k < netdata.length; k++)
- {
- if (netdata[k].length() > 0)
- {
- ethdata[j] = netdata[k];
- j++;
- }
- }
- }
- else if (line.startsWith(GPRSLINE))
- {
- netdata = segs[1].trim().split(" ");
- for (k = 0, j = 0; k < netdata.length; k++)
- {
- if (netdata[k].length() > 0)
- {
- gprsdata[j] = netdata[k];
- j++;
- }
- }
- }
- else if (line.startsWith(WIFILINE))
- {
- netdata = segs[1].trim().split(" ");
- for (k = 0, j = 0; k < netdata.length; k++)
- {
- if (netdata[k].length() > 0)
- {
- wifidata[j] = netdata[k];
- j++;
- }
- }
- }
- count++;
- }
-
- fstream.close();
- }
- catch (IOException e) {
- DisplayToast(e.toString());
- }
-
- }
-
- public String getinfo(String path)
- {
- File file;
- String str = "";
- FileInputStream in;
- try {
-
- file = new File(path);
- in = new FileInputStream(file);
-
- int length = (int) file.length();
- byte[] temp = new byte[length];
- in.read(temp, 0, length);
-
- str = EncodingUtils.getString(temp, TEXT_ENCODING);
-
- in.close();
- }
- catch (IOException e) {
- DisplayToast(e.toString());
- }
-
- return str;
- }
-
- public void writefile(String str, String path)
- {
- File file;
- FileOutputStream out;
- try {
-
- file = new File(path);
- file.createNewFile();
-
- out = new FileOutputStream(file);
- String infoToWrite = str;
-
- out.write(infoToWrite.getBytes());
-
- out.close();
- } catch (IOException e) {
-
- DisplayToast(e.toString());
- }
- }
-
- public void refresh()
- {
- readdev();
- data = ethdata[0] + "," + ethdata[1] + "," + ethdata[8] + ","
- + ethdata[9] + ","
- + gprsdata[0] + "," + gprsdata[1] + "," + gprsdata[8] + ","
- + gprsdata[9] + ","
- + wifidata[0] + "," + wifidata[1] + "," + wifidata[8] + ","
- + wifidata[9];
-
- String onstr = getinfo(ONPATH);
- String ondata[] = onstr.split(",");
-
-
- int[] delta = new int[12];
- delta[0] = Integer.parseInt(ethdata[0]) - Integer.parseInt(ondata[0]);
- delta[1] = Integer.parseInt(ethdata[1]) - Integer.parseInt(ondata[1]);
- delta[2] = Integer.parseInt(ethdata[8]) - Integer.parseInt(ondata[2]);
- delta[3] = Integer.parseInt(ethdata[9]) - Integer.parseInt(ondata[3]);
- delta[4] = Integer.parseInt(gprsdata[0]) - Integer.parseInt(ondata[4]);
- delta[5] = Integer.parseInt(gprsdata[1]) - Integer.parseInt(ondata[5]);
- delta[6] = Integer.parseInt(gprsdata[8]) - Integer.parseInt(ondata[6]);
- delta[7] = Integer.parseInt(gprsdata[9]) - Integer.parseInt(ondata[7]);
- delta[8] = Integer.parseInt(wifidata[0]) - Integer.parseInt(ondata[8]);
- delta[9] = Integer.parseInt(wifidata[1]) - Integer.parseInt(ondata[9]);
- delta[10] = Integer.parseInt(wifidata[8])- Integer.parseInt(ondata[10]);
- delta[11] = Integer.parseInt(wifidata[9])- Integer.parseInt(ondata[11]);
-
-
-
- final Calendar c = Calendar.getInstance();
- mYear = c.get(Calendar.YEAR);
- mMonth = c.get(Calendar.MONTH) + 1;
- mDay = c.get(Calendar.DAY_OF_MONTH);
- mHour = c.get(Calendar.HOUR_OF_DAY);
- mMinute = c.get(Calendar.MINUTE);
- mdate = mYear + "-" + mMonth + "-" + mDay;
- String text = getinfo(LOGPATH);
- String[] line = text.split("/n");
- String today = line[line.length - 1];
- String[] beToday = today.split(",");
-
-
- if (!beToday[0].equals(mdate))
-
- {
- text = text + mdate + ",0,0,0,0,0,0,0,0,0,0,0,0/n";
- writefile(text, LOGPATH);
- line = text.split("/n");
- today = line[line.length - 1];
- beToday = today.split(",");
- }
-
- int i;
-
- int[] newTodaydata = new int[12];
- String newtoday = mdate;
- for (i = 0; i <= 11; i++)
- {
- newTodaydata[i] = Integer.parseInt(beToday[i + 1]) + delta[i];
- newtoday = newtoday + "," + newTodaydata[i];
- }
-
- newtoday = newtoday + "/n";
- String[] beTotal = line[0].split(",");
- int[] newTotaldata = new int[12];
-
- String newtotal = "total";
- for (i = 0; i <= 11; i++)
- {
- newTotaldata[i] = Integer.parseInt(beTotal[i + 1]) + delta[i];
- newtotal = newtotal + "," + newTotaldata[i];
- }
-
- newtotal = newtotal + "/n";
-
- String before = "";
- for (i = 1; i <= line.length - 2; i++)
- before = before + line[i] + "/n";
-
- String newlog = newtotal + before + newtoday;
- writefile(data, ONPATH);
- writefile(newlog, LOGPATH);
- }
- }