2015年9月27日 星期日

偵測 CubieTruck CPU 溫度

觀察內核資訊
uname -a

# 我的是這樣:
Linux farm 3.4.79 #4 SMP PREEMPT Wed Sep 17 14:46:01 CST 2014 armv7l armv7l armv7l GNU/Linux
到路徑 /lib/modules/3.4.79/kernel/drivers/misc/ 查看是否有 sunxi-dbgreg.ko 這個檔案

載入內核模組
modprobe sunxi-dbgreg

建立 Script,有兩個參考腳本,請自行斟酌使用(我使用第二個)

腳本一:參考文章
#! /bin/sh

echo 'f1c25000:27003f' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25010:40000' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25018:10fff' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25004:10' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25020' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/read;

output=$(tail -n 1 /var/log/messages | awk '{print $7}')
degree=$(((output-1447)/10))
point=$(((output-1447)%10))
printf "Your CubieTruck's CPU current temperature is '\033[31m%d.%d\033[0m' degree Celsius.\n" $degree $point

腳本二:參考文章
#! /bin/sh

echo 'f1c25000:27003f' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25010:40000' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25018:10fff' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25004:10' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/write;
echo 'f1c25020' > /sys/devices/virtual/misc/sunxi-dbgreg/rw/read;

output=$(dmesg | tail -n 1 | awk '{print $2}')
degree=$(((output-1447)/10))
point=$(((output-1447)%10))
printf "Your CubieTruck's CPU current temperature is '\033[31m%d.%d\033[0m' degree Celsius.\n" $degree $point

存檔後記得修改檔案權限為 755

運行
Your CubieTruck's CPU current temperature is '39.0' degree Celsius.

設定開機自動載入,編輯 /etc/modules,在尾部添加:sunxi-dbgreg,這樣就可以在每次啟動的時候自動加載了


參考資料:
1. 如何得知 CubieTruck CPU 溫度
2. 獲取 CubieTruck CPU 溫度

[Ubuntu] 設定 啟動 snmp, net-snmp

安裝
~# apt-get update
~# apt-get install snmp
~# apt-get install snmp-mibs-downloader

編輯 snmpd.conf
~# echo 'rocommunity public' > /etc/snmp/snmpd.conf

理論上在 snmpd.conf 設定中加入這一行 rocommunity public 好像就已經足夠了,其他設定都可以註解起來也都沒關係一樣,算是一種快速的懶人設定法吧。

重新啟動snmpd
~# service snmpd restart

測試看看,如果跑出一堆訊息的話,就算是完成了。
~# snmpwalk -v 2c -c public localhost

用 snmp 輸出其他系統資訊 ~# snmpwalk -v 2c -c public localhost [OID參數]
# .1.3.6.1.4.1.2021.4.5.0 輸出記憶體的總容量
~# snmpwalk -v 2c -c public localhost .1.3.6.1.4.1.2021.4.5.0
UCD-SNMP-MIB::memTotalReal.0 = INTEGER: 2015680 kB

# .1.3.6.1.4.1.2021.4.6.0 輸出目前記憶體的使用量
~# snmpwalk -v 2c -c public localhost .1.3.6.1.4.1.2021.4.6.0
UCD-SNMP-MIB::memAvailReal.0 = INTEGER: 1525412 kB


其他可以用的 OID 參數
CPU的

Load
1 minute Load: .1.3.6.1.4.1.2021.10.1.3.1
5 minute Load: .1.3.6.1.4.1.2021.10.1.3.2
15 minute Load: .1.3.6.1.4.1.2021.10.1.3.3

CPU
percentage of user CPU time: .1.3.6.1.4.1.2021.11.9.0
raw user cpu time: .1.3.6.1.4.1.2021.11.50.0
percentages of system CPU time: .1.3.6.1.4.1.2021.11.10.0
raw system cpu time: .1.3.6.1.4.1.2021.11.52.0
percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0
raw idle cpu time: .1.3.6.1.4.1.2021.11.53.0
raw nice cpu time: .1.3.6.1.4.1.2021.11.51.0
-------------------------------------------------------------------------------
記憶體的

Total Swap Size: .1.3.6.1.4.1.2021.4.3.0
Available Swap Space: .1.3.6.1.4.1.2021.4.4.0
Total RAM in machine: .1.3.6.1.4.1.2021.4.5.0
Total RAM used: .1.3.6.1.4.1.2021.4.6.0
Total RAM Free: .1.3.6.1.4.1.2021.4.11.0
Total RAM Shared: .1.3.6.1.4.1.2021.4.13.0
Total RAM Buffered: .1.3.6.1.4.1.2021.4.14.0
Total Cached Memory: .1.3.6.1.4.1.2021.4.15.0
-------------------------------------------------------------------
硬碟的

The snmpd.conf needs to be edited. Add the following (assuming a machine with a single ‘/’ partition):

disk / 100000 (or)

includeAllDisks 10% for all partitions and disks

The OIDs are as follows

Path where the disk is mounted: .1.3.6.1.4.1.2021.9.1.2.1
Path of the device for the partition: .1.3.6.1.4.1.2021.9.1.3.1
Total size of the disk/partion (kBytes): .1.3.6.1.4.1.2021.9.1.6.1
Available space on the disk: .1.3.6.1.4.1.2021.9.1.7.1
Used space on the disk: .1.3.6.1.4.1.2021.9.1.8.1
Percentage of space used on disk: .1.3.6.1.4.1.2021.9.1.9.1
Percentage of inodes used on disk: .1.3.6.1.4.1.2021.9.1.10.1

----------------------------------------------------------------
以下是windows下常用的OID
CPU
.1.3.6.1.2.1.25.3.3.1.2
如果有一個雙核cpu則有2個值,顯示當前負載

記憶體,硬碟
記憶體跟硬碟都統一在mib2.host.hrStorage裡 1.3.6.1.2.1.25.2
hrStorageType節點,hrStorageFixedDisk表示硬碟,hrStorageVirtualMemory是虛擬記憶體,hrStorageRam是記憶體
根據id號查:
hrStorageAllocationUnits是簇的大小
hrStorageSize是有多少簇,兩者想乘就是總容量
hrStorageUsed是已使用多少,跟總容量相除就是佔用率

---------------------------------------------------------------

系統執行時間
System Uptime: .1.3.6.1.2.1.1.3.0
註:linux系統的話也可直接用uptime命令獲得

網路設備的
ifIndex 1.3.6.1.2.1.2.2.1.1.0 埠索引號
ifDescr 1.3.6.1.2.1.2.2.1.2.0 埠描述
ifType 1.3.6.1.2.1.2.2.1.3.0 埠類型
ifMtu 1.3.6.1.2.1.2.2.1.4.0 最大傳輸包位元組數
ifSpeed 1.3.6.1.2.1.2.2.1.5.0 埠速度
ifPhysAddress 1.3.6.1.2.1.2.2.1.6.0 物理位址
ifOperStatus 1.3.6.1.2.1.2.2.1.8.0 操作狀態
ifLastChange 1.3.6.1.2.1.2.2.1.9.0 上次狀態更新時間
ifInOctets 1.3.6.1.2.1.2.2.1.10.0 輸入位元組數
ifInUcastPkts 1.3.6.1.2.1.2.2.1.11.0 輸入非廣播包數
ifInNUcastPkts 1.3.6.1.2.1.2.2.1.12.0 輸入廣播包數
ifInDiscards 1.3.6.1.2.1.2.2.1.13.0 輸入包丟棄數
ifInErrors 1.3.6.1.2.1.2.2.1.14.0 輸入包錯誤數
ifInUnknownProtos 1.3.6.1.2.1.2.2.1.15.0 輸入未知協定包數
ifOutOctets 1.3.6.1.2.1.2.2.1.16.0 輸出位元組數
ifOutUcastPkts 1.3.6.1.2.1.2.2.1.17.0 輸出非廣播包數
ifOutNUcastPkts 1.3.6.1.2.1.2.2.1.18.0 輸出廣播包數
ifOutDiscards 1.3.6.1.2.1.2.2.1.19.0 輸出包丟棄數
ifOutErrors 1.3.6.1.2.1.2.2.1.20.0 輸出包錯誤數
ifOutQLen 1.3.6.1.2.1.2.2.1.21.0 輸出隊長

流量輸出的是計數器,單位是byte,32位元
如果流量過大出現統計不正確的情況,就需要編譯時加上enable-mfd-rewrites參數,來支持64位的計數器

參考資料
1. 利用net-snmp讀取windows,linux,snmp設備資料(cpu,記憶體,網卡)
2. SNMP – The Missing MIB
3.

2015年9月26日 星期六

[Ubuntu] 關閉 停用 irqbalance

關閉 停用 irqbalance

編輯 ~# vi /etc/default/irqbalance 修改以下設定:
# 找到下面這行設定
#ENABLED="1"
# 更改設定為0
ENABLED="0"


關閉 irqbalance 服務
~# service irqbalance stop
irqbalance stop/waiting


參考資料:
1. 深度剖析告訴你irqbalance有用嗎?
2. linux irqbalance 優化中斷
3. linux irqbalance 是做什麼的
4. Linux irqbalance、中斷和進程綁定CPU
5. 使用irqbalance服務實現中斷負載均衡
6. How do I disable irqbalance?



[Ubuntu] MRTG 2.x 安裝 Step by Step

apt-get install snmp snmpd
apt-get install mrtg

[snmpd.conf] 
先備份 
# cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak 

編輯 snmpd.conf 
# echo 'rocommunity public' > /etc/snmp/snmpd.conf 

很多人mrtg裝不起來,大多是因為snmpd.conf弄不好,不過我發現到一個可以偷吃步的方法!! 
打上這一行(注意空格),就全部ok嘍,夠輕鬆愉快吧!! 


重跑snmp 
# /etc/init.d/snmpd restart 

測試一下snmp 
snmpwalk localhost -c public -v 1 

跑出一堆訊息就成功嘍!! 

... 
... 
HOST-RESOURCES-MIB::hrSWRunPerfMem.21022 = INTEGER: 1304 KBytes 
HOST-RESOURCES-MIB::hrSWRunPerfMem.21023 = INTEGER: 1752 KBytes 
HOST-RESOURCES-MIB::hrSWRunPerfMem.26812 = INTEGER: 14788 KBytes 
IPV6-MIB::ipv6Forwarding.0 = INTEGER: notForwarding(2) 
IPV6-MIB::ipv6DefaultHopLimit.0 = INTEGER: 0 
---------------------------------------------------------------------- 
snmp搞好了,就成功一半嘍,其他的就沒啥大礙了! 

[安裝mrtg] 
apt-get install mrtg 

會詢問妳 
Make /etc/mrtg.conf owned by and readable only by the MRTG user? 
選擇Yes 

---------------------------------------------------------------------- 
[產生mrtg.cfg] 
cfgmaker --global "WorkDir: /var/www/mrtg" --global "Refresh: 600" --global "Interval: 5" --global "WriteExpires: Yes" --global "Language: big5" --global "options[_]: bits" --ifdesc=descr --ifref=descr public@127.0.0.1 > /etc/mrtg.cfg 

整行複製吧,不斷行!! 
---------------------------------------------------------------------- 
執行mrtg 
#mrtg /etc/mrtg.cfg 

執行3次,無錯誤訊息即可 

---------------------------------------------------------------------- 
[產生流量分析網頁index.html] 
indexmaker --columns=1 \ 
--output=/var/www/mrtg/index.html \ 
--title='Traffic Info' \ 
/etc/mrtg/mrtg.cfg 

或是... 
indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html 

以上都一樣是產生 index.html 
---------------------------------------------------------------------- 
[觀看mrtg 流量分析表囉] 
http://your.domain.name/mrtg/index.html 

以上這樣就會有基本的 eth0 的流量分析了 

2015年9月22日 星期二

[Ubuntu] 停用 snmp 擾人的 log Message Connection from UDP: [127.0.0.1]:43976->[ 127.0.0.1]:161

不可否認的 snmp 會一直在 syslog 產生以下這類的訊息,大概 5 分鐘就會產生一條,一天24小時就會產生288條訊息,這的確是一種無止盡的嘮叨。

Sep 22 18:20:03 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:43799->[127.0.0.1]:161
Sep 22 18:25:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:44170->[127.0.0.1]:161
Sep 22 18:30:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:38358->[127.0.0.1]:161
Sep 22 18:35:03 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:41847->[127.0.0.1]:161
Sep 22 18:40:03 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:38820->[127.0.0.1]:161
Sep 22 18:45:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:57794->[127.0.0.1]:161
Sep 22 18:50:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:46374->[127.0.0.1]:161
Sep 22 18:55:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:59770->[127.0.0.1]:161
Sep 22 19:00:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:53427->[127.0.0.1]:161
Sep 22 19:05:03 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:56370->[127.0.0.1]:161
Sep 22 19:10:03 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:42871->[127.0.0.1]:161
Sep 22 19:15:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:55832->[127.0.0.1]:161
Sep 22 19:20:02 farm snmpd[25909]: Connection from UDP: [127.0.0.1]:45324->[127.0.0.1]:161


以下設定可以把 snmp 這類的訊息關閉

打開編輯 vi /etc/default/snmpd 加入:
# 將這一行註解起來
# SNMPDOPTS='-Lsd -Lf /dev/null -u snmp -g snmp -I -smux,mteTrigger,mteTriggerConf -p /var/run/snmpd.pid'

# 加入底下這一行
SNMPDOPTS='-Lf /dev/null -u snmp -I -smux -p /var/run/snmpd.pid 127.0.0.1'

存檔,重啟 service snmpd restart 這樣你就再也看不到這些惱人的訊息了。

參考文件:snmpd[]: Connection from UDP: [127.0.0.1]:


2015年9月7日 星期一

C# DataRelation 類別

private void Form1_Load ( object sender, EventArgs e )
{
    string comm_Supplier = "select * from supplier";
    string comm_products = "select * from products";
    MySqlConnection conn = clsdb.getcon();
    
 try
    {
        // 是否載入資料庫的關連
  //da_Supplier.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        da_Supplier.SelectCommand = new MySqlCommand(comm_Supplier, conn);
        da_Supplier.Fill(ds, "Supplier");
        da_Products.SelectCommand = new MySqlCommand(comm_products, conn);
        da_Products.Fill(ds, "Products");

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    
    // 設定DataGridView資料來源
    bs_Supplier.DataSource = ds;
    bs_Supplier.DataMember = "Supplier";
    dgv_supplier.DataSource = bs_Supplier;
    
    // 初始化DataGridView
    InitializeDataGridView(dgv_supplier);

    bindingNavigator1.BindingSource = bs_Supplier;

    // DataTable建立關連
    DataColumn x = ds.Tables["Supplier"].Columns["sup_Num"];
    DataColumn y = ds.Tables["Products"].Columns["sup_Num"];
    rel = new DataRelation("pro", x, y, false);
    ds.Relations.Add(rel);

    // 設定DataGridView資料來源
    bs_Products.DataSource = bs_Supplier;
    bs_Products.DataMember = "pro";
    dgv_products.DataSource = bs_Products;
}


參考資料
1:DataRelation 類別

2015年9月6日 星期日

在 Blogger 上面增加顯示程式碼的 Code 區域

在 Blogger 增加工具


  • 進入「版面配置」,
  • 按下「新增小工具」,選擇「HTML/JavaScript」,
  • 然後直接在「內容」區塊裡面輸入任何想要的 script, style 標籤即可。

<style>
pre.code {
  display: block;
  font-size: 10pt;
  overflow: auto;
  max-height:1000px;
  padding: 10px 10px 10px 50px;
  border: 1px solid #ccc;
  background: #f8f8ff url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAASwCAYAAAAt7rCDAAAABHNCSVQICAgIfAhkiAAAAQJJREFUeJzt0kEKhDAMBdA4zFmbM+W0upqFOhXrDILwsimFR5pfMrXW5jhZr7PwRlxVX8//jNHrGhExjXzdu9c5IiIz+7iqVmB7Hwp4OMa2nhhwN/PRGEMBh3Zjt6KfpzPztxW9MSAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzB8HS+J9kUTvzEDMwAAAABJRU5ErkJggg==) left top repeat-y;
}

pre.codeline {
  display: block;
  font-size: 10pt;
  overflow: auto;
  padding: 0 10px 0 18px;
  border: 1px solid #ccc;
  background: #f8f8ff url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAASwCAYAAAAt7rCDAAAABHNCSVQICAgIfAhkiAAAAQJJREFUeJzt0kEKhDAMBdA4zFmbM+W0upqFOhXrDILwsimFR5pfMrXW5jhZr7PwRlxVX8//jNHrGhExjXzdu9c5IiIz+7iqVmB7Hwp4OMa2nhhwN/PRGEMBh3Zjt6KfpzPztxW9MSAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzB8HS+J9kUTvzEDMwAAAABJRU5ErkJggg==) left top repeat-y;
}

code.prettyprint {
  line-height: 1.2em;
  margin: 1px;
}

code.inline {
  background: rgb(255, 255, 255);
  font-family: Consolas;
  font-size: 14px;
  border-radius: 0.4em;
  border: 1px solid rgb(221, 221, 221);
  color: #555555;
  display: inline-block;  
  line-height: 1.5em;
  margin: -1px 0px;
  padding: 0px 0.3em;
}

.sheet {
  style=background-color: #fefdfa;
  border-radius: 3px;
  border: 1px solid rgb(204, 204, 204);
  box-shadow: rgba(0, 0, 0, 0.439216) 0px 1px 4px 0px;
  color: #333333;
  font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;
  font-size: 13px;
  line-height: 18.2px;
  margin: 8px 0px;
  padding: 8px;
}

ul {
  style=line-height: 1.4;
  margin: 0.5em 0px;
  padding: 0px 2.5em;
}

li {
  style="border: none;
  margin: 0px 0px 0.25em;
  padding: 0.25em 0px;
}
</style>

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>


程式碼顯示測試一:行號顯示


<pre class="codeline"><code class="prettyprint linenums">
  ...打入要顯示的Code...
  ...打入要顯示的Code...
  ...打入要顯示的Code...
</code></pre>
public class HelloWorld {
    public static void main (String[] args) {
        System.out.println("Hello, world!");
        System.out.println("Hello, world!");
        System.out.println("Hello, world!");
    }
}


程式碼顯示測試二:正常顯示(無行號)


<pre class="code"><code class="prettyprint">
  ...打入要顯示的Code...
  ...打入要顯示的Code...
  ...打入要顯示的Code...
</code></pre>
public class HelloWorld {
    public static void main (String[] args) {
        System.out.println("Hello, world!");
    }
}


程式碼顯示測試三:無加框


<pre><code class="prettyprint">
  ...打入要顯示的Code...
  ...打入要顯示的Code...
  ...打入要顯示的Code...
</code></pre>
public class HelloWorld {
    public static void main (String[] args) {
        System.out.println("Hello, world!");
    }
}

程式碼顯示測試四:行內加框


<code class="inline">code class="inline"</code>

頭文字 code class="inline" 尾文字




程式碼顯示測試五


加強 <ul> / <li> 標籤效果
  • ...行列效果...
  • ...行列效果...
  • ...行列效果...

<dir class="sheet">
<ul>
<li>...行列效果...</li>
<li>...行列效果...</li>
<li>...行列效果...</li>
</ul>
</div>

參考文件:
1:在 Blogger 上用 Google Code Prettify 及 GitHub Gist 顯示程式碼 (不修改範本的懶人招數)
2:在網頁中嵌入顯示程式碼:CSS Block
3:程式碼高亮

處理 Html 原始碼標籤
1:Javascript Tools 選 JS Encode,Decode
2:opinionatedgeek



MySQL 設定時區

打開 my.ini

[mysqld] 底下加入這一行設定:

default-time-zone = '+8:00'

存檔,重新啟動 MySQL

參考文件:
1:http://ppt.cc/0AgCa

2015年9月5日 星期六

C# + MySQL 設定編碼,解決中文亂碼問題

使用 C# + MySQL 的組合,必須設定先 MySQL 的編碼,不然會錯誤連連

打開 MySQL 的設定檔 my.ini

找到 [client] 這個欄位加入下面設定:

default-character-set = utf8


找到 [mysqld] 這個欄位加入下面設定:

character_set_server=utf8
collation_server=utf8_general_ci


存檔,重新啟動 MySQL

這樣就可以解決中文亂碼的問題了。

參考文件:
1:http://ppt.cc/LvEJl