<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nattster &#187; 3g</title>
	<atom:link href="http://nattster.siamdev.net/tag/3g/feed/" rel="self" type="application/rss+xml" />
	<link>http://nattster.siamdev.net</link>
	<description>ลั่ลล้า...</description>
	<lastBuildDate>Sun, 25 Dec 2011 05:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Ubuntu 3G ด้วย DTAC Aircard + Data Counter</title>
		<link>http://nattster.siamdev.net/2010/03/ubuntu-3g-data-counter/</link>
		<comments>http://nattster.siamdev.net/2010/03/ubuntu-3g-data-counter/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 04:46:53 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[3g]]></category>
		<category><![CDATA[data counter]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=654</guid>
		<description><![CDATA[วิธีตั้งค่า 1. ใส่ Sim card แล้วเสียบ Aircard เข้ากับคอม 2. รอ Aircard เชื่อมต่อเขากับเครือข่าย (ไฟกระพริบสีฟ้า) 3. ลอง lsusb จะเจอ Huawei Technologies Co., Ltd. E620 USB Modem 4. คลิกขวาที่ Network Manager เลือก Edit Connections&#8230; 5. คลิกที่ Tab Mobile Broadband กดปุ่ม Add 6. จะปรากฏหน้าต่างดังนี้ เลือก Device เป็น HUAWEI Technology HUAWEI Mobile แล้วกด Forward 7. เลือก Thailand กด Forward อีกรอบ [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nattster.siamdev.net/wp-content/uploads/2010/03/2010-03-10-18.18.58.jpg" rel="lightbox[654]" title="2010-03-10 18.18.58"><img src="http://nattster.siamdev.net/wp-content/uploads/2010/03/2010-03-10-18.18.58.jpg" alt="" title="2010-03-10 18.18.58" width="150" height="175" class="alignleft size-full wp-image-662" /></a></p>
<h3>วิธีตั้งค่า</h3>
<p>1. ใส่ Sim card แล้วเสียบ Aircard เข้ากับคอม<br />
2. รอ Aircard เชื่อมต่อเขากับเครือข่าย (ไฟกระพริบสีฟ้า)<br />
3. ลอง lsusb จะเจอ Huawei Technologies Co., Ltd. E620 USB Modem<br />
4. คลิกขวาที่ Network Manager เลือก Edit Connections&#8230;<br />
<span id="more-654"></span><br />
<a href="http://nattster.siamdev.net/wp-content/uploads/2010/03/step1.png" rel="lightbox[654]" title="step1"><img src="http://nattster.siamdev.net/wp-content/uploads/2010/03/step1.png" alt="" title="step1" width="326" height="156" class="aligncenter size-full wp-image-658" /></a><br />
5. คลิกที่ Tab Mobile Broadband กดปุ่ม Add<br />
6. จะปรากฏหน้าต่างดังนี้ เลือก Device เป็น HUAWEI Technology HUAWEI Mobile แล้วกด Forward<br />
<a href="http://nattster.siamdev.net/wp-content/uploads/2010/03/step2.png" rel="lightbox[654]" title="step2"><img src="http://nattster.siamdev.net/wp-content/uploads/2010/03/step2-300x204.png" alt="" title="step2" width="300" height="204" class="aligncenter size-medium wp-image-659" /></a><br />
7. เลือก Thailand กด Forward อีกรอบ<br />
8. เลือก I can&#8217;t find my provider&#8230; แล้วตั้งชื่อ เช่น i-mobile 3GX แล้วก็กด Forward<br />
9. กำหนด APN (Access Point Name) ของเครือข่ายผมใช้ Internet แล้วก็กด Forward<br />
10. กด Apply อีก 2 ครั้ง<br />
11. ตอนนี้เวลาคลิกที่ไอคอน Network Manager จะปรากฏรายชื่อเครือข่าย Mobile Broadband ให้คลิกเพื่อเชื่อมต่อได้แล้วครับ<br />
<a href="http://nattster.siamdev.net/wp-content/uploads/2010/03/step3.png" rel="lightbox[654]" title="step3"><img src="http://nattster.siamdev.net/wp-content/uploads/2010/03/step3-300x203.png" alt="" title="step3" width="300" height="203" class="aligncenter size-medium wp-image-660" /></a></p>
<h3>ปัญหาที่เจอ</h3>
<p>	เวลาใช้บน Ubuntu เราไม่มีโปรแกรมช่วยต่อแบบบน Windows ตัว Aircard เลยเลือกเชื่อมต่อเครือข่ายที่สัญญาณแรงที่สุด แถวหอผม สัญญาณ TOT 3G บางทีก็เบา ตัว Aircard มันก็เลยต่อ AIS GSM ให้ พอ connect ปั๊บก็ใช้เน็ตไม่ได้ เพราะผิดเครือข่าย<br />
	วิธีแก้คือ Disconnect แล้วก็ถอด/เสีย Aircard ใหม่อีกรอบครับ</p>
<h3>Data Counter สำหรับ Ubuntu</h3>
<p>	คนใช้ 3G ทุกคนคงต้องกังวลเรื่อง Bandwidth (ปริมาณข้อมูลที่รับส่งในแต่ละเดือน) บน Ubuntu สามารถดูปริมาณอัพโหลด/ดาวน์โหลดได้ด้วยคำสั่ง ifconfig ppp0 ดังนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ifconfig</span> ppp0</pre></div></div>

<p>แต่ถ้าต้องนั่งจดเองทุกครั้งที่ต่อเน็ต ก็คงเหนื่อยเนาะครับ (ผมทำอยู่เดือนนึง หะๆ)</p>
<p>ผมเลยเขียน script เล็กๆ ไปฝากไว้เป็น Network Manager dispatcher ช่วยเก็บ log bandwidth ให้อัตโนมัติทุกครั้งที่ disconnect ครับ</p>
<p>สร้างไฟล์ 02datacounter ไว้ที่ /etc/NetworkManager/dispatcher.d / ให้มีเนื้อหาดังนี้<br />
ใช้คำสั่ง</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gedit <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>NetworkManager<span style="color: #000000; font-weight: bold;">/</span>dispatcher.d<span style="color: #000000; font-weight: bold;">/</span>02datacounter</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash </span>
<span style="color: #007800;">IF</span>=<span style="color: #007800;">$1</span> 
<span style="color: #007800;">STATUS</span>=<span style="color: #007800;">$2</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$IF</span>&quot;</span> = <span style="color: #ff0000;">&quot;ppp0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> 
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$STATUS</span>&quot;</span> = <span style="color: #ff0000;">&quot;down&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> 
        <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>messages <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">'pppd.*Sent.*received'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>nattster<span style="color: #000000; font-weight: bold;">/</span>logs<span style="color: #000000; font-weight: bold;">/`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>b<span style="color: #000000; font-weight: bold;">`</span>.txt 
    <span style="color: #000000; font-weight: bold;">fi</span> 
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>Script ข้างบนจะเก็บ log ไว้ในโฟลเดอร์ /home/nattster/logs/ ครับ โดยแยกตามเดือน เช่น Jan.txt, Feb.txt, Mar.txt, …</p>
<p>สร้างไฟล์ /home/nattster/logs/report.py มีเนื้อหาตามนี้ครับ (ใช้วิเคราะห์ log สร้างรายงานการใช้ bandwidth)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># by Natt Piyapramote (@nattster)</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">glob</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> analyze<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
	p = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'([A-Za-z]+ [ 0-9]+) .* Sent (.*) bytes, received (.*) bytes.'</span><span style="color: black;">&#41;</span>
&nbsp;
	f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	m = p.<span style="color: black;">findall</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
&nbsp;
	bw = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
	sumbw = <span style="color: #ff4500;">0</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> m:
		day, up, down = x
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> bw.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>day<span style="color: black;">&#41;</span>:
			bw<span style="color: black;">&#91;</span>day<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">0</span>
		bw<span style="color: black;">&#91;</span>day<span style="color: black;">&#93;</span> += <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>up<span style="color: black;">&#41;</span>+<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>down<span style="color: black;">&#41;</span>
		sumbw += <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>up<span style="color: black;">&#41;</span>+<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>down<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> day <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>bw.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'   %s bw = %.2f MB'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>day,<span style="color: black;">&#40;</span>bw<span style="color: black;">&#91;</span>day<span style="color: black;">&#93;</span>/<span style="color: #ff4500;">1024.0</span>/<span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> filename, <span style="color: #483d8b;">'Total = %.2f MB'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>sumbw/<span style="color: #ff4500;">1024.0</span>/<span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">glob</span>.<span style="color: #dc143c;">glob</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'*.txt'</span><span style="color: black;">&#41;</span>:
	analyze<span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span></pre></div></div>

<p>เมื่อลองรันจะได้ผลลัพธ์ดังนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python report.py 
   Mar  <span style="color: #000000;">1</span> bw = <span style="color: #000000;">75.78</span> MB
   Mar  <span style="color: #000000;">2</span> bw = <span style="color: #000000;">8.20</span> MB
   Mar  <span style="color: #000000;">3</span> bw = <span style="color: #000000;">81.57</span> MB
   Mar  <span style="color: #000000;">4</span> bw = <span style="color: #000000;">97.50</span> MB
   Mar  <span style="color: #000000;">5</span> bw = <span style="color: #000000;">1.85</span> MB
   Mar  <span style="color: #000000;">6</span> bw = <span style="color: #000000;">21.89</span> MB
   Mar  <span style="color: #000000;">7</span> bw = <span style="color: #000000;">41.43</span> MB
   Mar  <span style="color: #000000;">8</span> bw = <span style="color: #000000;">13.83</span> MB
   Mar  <span style="color: #000000;">9</span> bw = <span style="color: #000000;">100.00</span> MB
   Mar <span style="color: #000000;">10</span> bw = <span style="color: #000000;">3.77</span> MB
   Mar <span style="color: #000000;">11</span> bw = <span style="color: #000000;">77.11</span> MB
Mar.txt Total = <span style="color: #000000;">522.94</span> MB</pre></div></div>

<h3>หมายเหตุ:</h3>
<p>- อย่าลืมเปลี่ยน /home/nattster/ เป็น home directory ของคุณ หรือ path อื่นๆ ที่จะเก็บ log<br />
- แก้ ppp0 เป็นชื่อ interface ที่ถูกต้องด้วยนะครับ (ดูรายชื่อ interface ได้ด้วยคำสั่ง ifconfig ครับ)<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2010/03/ubuntu-3g-data-counter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

