แสดงกระทู้

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - golfreeze

Pages: 1 ... 135 136 [137] 138 139 ... 146
2041
W: There is no public key available for the following key IDs:
4D270D06F42584E6
E: Dynamic MMap ran out of room
E: Error occurred while processing wesnoth-all (NewVersion1)
E: Problem with MergeList
/var/lib/apt/lists/ftp.coe.psu.ac.th_debian_dists_stable_main_binary-i386_Packages
E: The package lists or status file could not be parsed or opened.

แก้โดย :

# gpg --keyserver pgpkeys.mit.edu --recv-key 4D270D06F42584E6

# gpg -a --export 4D270D06F42584E6 | sudo apt-key add -

2042
ถ้าจำเป็น ที่จะต้องระบุ email account จากการรัน crontab ก็ใช้คู่กับ sendmail ดูนะครับ

#crontab -e
55 23 * * * /usr/local/bin/pflogsumm -d today /var/log/maillog | /usr/sbin/sendmail -F
"root@xxx.com"; -f root@xxx.com -s "`uname -n` daily mail stats"
email@ปลายทาง.com

2043
สำหรับสาวก Postfix ครับ

วันนี้มี tools สำหรับเช๊ค email stat traffic มาฝากกันครับ ซึ่งผมได้ลองใช้มาสักพักใหญ่ๆ รู้สึกชอบ
อิอิ

ตัว stat traffic ตัวนี้จะเอา log ของ mail log มาทำการวิเคราะห์นะครับ ซึ่งจะมีปริมาณ การรับ การส่ง
เมลในแต่ละวัน ซึ่งเป็นเมลที่ drop reject หรือ ban ก็ดูได้หมดครับ หรือจะดูเป็น stat ย้อนหลังก็ได้

#freebsd
Packetlove# make search name="pflogsumm"
Port: pflogsumm-1.1.2,1
Path: /usr/ports/mail/pflogsumm
Info: Postfix Log Entry Summarizer
Maint: flo@kasimir.com
B-deps: perl-5.8.9_3
R-deps: p5-Bit-Vector-7.1 p5-Carp-Clan-6.04 p5-Date-Calc-6.3 perl-5.8.9_3
WWW: http://jimsun.LinxNet.com/postfix_contrib.html

install โดย
#cd /usr/ports/mail/pflogsumm
#make install clean

crontab รันโดย
#crontab -e

45 11 * * * /usr/local/bin/pflogsumm -d today /var/log/maillog | /usr/bin/mailx -s "`uname -n` daily mail stats" receiver@email.com

=========================================

##Debian

#apt-get install pflogsumm

แล้วก็รันใน crontab ดังนี้ครับ

#crontab -e
45 11 * * * /usr/sbin/pflogsumm -d today /var/log/mail.log | /usr/bin/mailx -s "`uname -n` daily mail stats" receiver@email.com

โดยที่ receiver@email.com คือ emailปลายทางที่จะให้ระบบgen stat ส่งไปนะครับ

ยังไงก็ลองใช้กันดูนะครับ อิอิ

2044
ไฟล์ที่ต้อง copy คือ

/etc/passwd
/etc/group
/etc/master.passwd

หรือถ้าเลือก user มาก็เลือกมาให้เหมือนกันด้วยน่ะครับ ทั้งสามไฟล์

เสร็จแล้วใช้คำสั่ง

pwd_mkdb -p /etc/master.passwd

เพื่อ rebuilld password .

2045
#on debian
bonnie++ -d /path_check -s 8192 -r 2048 -n 512 -u 0:0

2046
An Introduction to Shell Scripting with bash (continued)

In the last article, we dealt with variables and positional parameters. This month, it's time to
examine the more advanced features that make scripts much more powerful, especially bash's flow
control capabilities.

Functions

The bash shell allows us to define functions, which are effectively subroutines. This makes complex
scripts significantly simpler, as well as making them faster. You can also define functions in your
login scripts, rather like aliases, only more sophisticated. You can define a function like this:

funcname ()
{
shell statements
}

You can do this interactively - for example:

[ les@sleipnir les]$ beep ()
> {
> echo -e -n \\a
> }
[ les@sleipnir les]$ beep
[ les@sleipnir les]$

You can see how, after the parentheses, the shell prompt changes to PS2 (>) to indicate the shell
will accept more input without immediately processing it, until the closing curly brace. Once this
has been done, you can execute a beep command at any time. (In case you're wondering, aliases
take precedence over keywords like if, which take precedence over functions, which take precedence
over builtins (like cd) which take precedence over executable files).

Flow Control

A shell script that simply runs some commands is all very well, but it would be a lot better if it
could deal with errors rather than plowing on regardless. And looping would be useful - to process
lots of files or directories.

if ... else

Almost all programs that run from the command line terminate through an exit() system call which
returns an exit status. In general, an exit status value of 0 indicates successful execution, while
increasingly large integers indicate increasingly serious errors.

Immediately after a program returns, its exit status can be accessed through the $? shell variable.
For example:

[ les@sleipnir scripts]$ ls
stat
[ les@sleipnir scripts]$ echo $?
0
[ les@sleipnir scripts]$ ls nonexistent
ls: nonexistent: No such file or directory
[ les@sleipnir scripts]$ echo $?
1
[ les@sleipnir scripts]$

As you can see, if the ls command matches some files, its exit status is zero, while if no files are
found, the exit status is one. The exit status can be used in scripts - for example, here is an
excerpt from a shell script that I commonly use to rebuild kernels:

make bzImage
if [ $? -gt 0 ] ; then
echo make bzImage failed
exit
fi

This introduces the if statement, as well as the -gt comparison operator. The syntax of if is:

if COMMANDS ; then
COMMANDS;
[ elif COMMANDS; then COMMANDS ; ]...
[ else COMMANDS; ]
fi

The square brackets in the syntax above indicate optional components of the statement. However,
square brackets, to the bash shell - as in the preceding example - indicate a test (in fact,
/usr/bin/[ is a symbolic link to the test binary on many systems). The expression in the square
brackets will be evaluated and true or false returned. So in the example above, if the make bzImage
step fails, it will return an exit status of 1 or higher. The test says that if the exit status is
greater than zero, then the script will print an error message and exit.

You can also test files for various attributes: does the file exist, is it a directory, is it
executable, do you have read permission, etc. This is particularly useful in scripts that install
software - you can check whether a configuration file already exists, for example, and skip any
steps that would over-write it.

Table 1: File operators
Operator Returns true if:
-d file file exists and is a directory
-e file file exists
-f file file exists and is a regular file
-r file file exists and you have read permission on it
-s file file exists and is more than 0 bytes in size
-w file You have write permission on file
-x file You have execute permission on file, or search permission if it is a directory
-O file You are the owner of file
-G file You are a member of the group that owns the file
file1 -nt file2 file1 is newer than file2
file1 -ot file2 file1 is older than file2
Remember that many of these commands can be used interactively, at the command prompt, as well as in
scripts. For example, here is a command line that will show the disk space used by every
subdirectory of the current directory:

for n in * ; do if [ -d $n ]; then du -hs $n ; fi; done

You can also perform string comparisons, as shown in Table 2.

Table 2: String operators
Operator Returns true if:
str1 = str2 str1 matches str2
str1 != str2 str1 does not match str2
str1 < str2 str1 is less than str2
str1 > str2 str1 is greater than str2
-n str1 str1 is non-null (i.e. is not an empty string)
-z str1 str1 is null (i.e. has length zero)
And finally, you can make comparisons between integer variables, as shown in Table 3. You'll see
a little later how to perform arithmetic.

Table 3: Integer operators
Operator Returns true if:
var1 -lt var2 var1 is less than var2
var1 -le var2 var1 is less than or equal to var2
var1 -eq var2 var1 is equal to var2
var1 -ne var2 var1 is not equal to var2
var1 -gt var2 var1 is greater than var2
var1 -ge var2 var1 is greater than or equal to var2

The for Statement

The for statement provides looping within scripts. However, bash's for statement is quite
different from that found in C, Perl and similar languages, which surprises novice shell scripters.
The syntax of the for statement is:

for varname [in list]
do
statement
...
done

The statements in the loop will probably use $varname in some way. The effect of this statement is
to substitute each value in list in turn, into the variable varname, and perform the body of the
loop. The list can actually be a wildcard, which the shell will automatically expand into a list of
matching filenames, and you can see an example of this in the directory size listing command above.

case

The case statement is essentially a multi-way branch, rather like an if . . ifelif . . . else . . .
fi chain. It is particularly common in the SysVInit scripts which are used to start, stop, restart
and reload daemons on distributions like Red Hat and Mandrake. The basic syntax of the case
statement is:

case varname in
value1)
statements
;;
value2)
statements
;;
*)
statements
;;
esac

You can find lots of examples in the / etc/rc.d/init.d or /etc/init.d directory on your system, and
the example at the end of this article.

while & until

More conventional are the while and until looping constructs. The syntax for while is:

while COMMANDS ; do
COMMANDS
done

Here is an example - a simple script that adds up the values of all integers between 1 and 100:

Listing 1: A shell script to sum the integers between 1 and 100:

#!/bin/bash
# Simple script to demonstrate while and arithmetic
count=0
sum=0
while [ $count -lt 101 ] ; do
sum=$(( $sum + $count ))
count=$(( $count + 1 ))
done
echo "Sum = $sum"

This example also shows one way of doing arithmetic in shell scripts, with the $(( expression ))
construct.

The until statement is similar, except that it continues looping as long as the test condition
fails. I find this quite useful at the command line, as a way of waiting for certain events to occur
- for example, waiting for a network link to come up:

until ping -c 1 192.168.170.1; do sleep 60; done; ssh 192.168.170.1

shift

Another statement that is useful in connection with loops is shift. This works on Linux pretty much
like it does on DOS/Windows - it shifts the positional parameters (except $0) down by one, so that
the old value of $1 is lost and its new value is $2, the new value of $2 is $3, and so on. This
allows one-by-one processing of an arbitrarily long list of arguments. Here is an example that
demonstrates shift, along with the until statement:

Listing 2: A script to demonstrate shift.

#!/bin/bash
until [ -z $1 ] ; do
echo $1
shift
done

This simple script just echoes its arguments, each on a separate line.

Let's finish up with an example. This short script works to remind you to do things at some time
(between 1 and 999 minutes) in the future, and illustrates many of the techniques previously
discussed:

* Integer operator ( -lt )
* Definition and calling of a function
* A for loop, if statement and more integer operators ( -eq)
* A case statement


Listing 3: A short script to set a reminder timer

#!/bin/bash
# Simple reminder shell script
if [ $# -lt 2 ] ; then
echo "usage: $0 minutes message"
exit 1
fi

alarm()
{
sleep ${delay}m
echo -n $msg
for i in 1 2 3 4 5 6 7 8 9 ; do
if [ $i -eq 2 -o $i -eq 4 -o $i -eq 6 -o $i -eq 8 ] ; then
sleep 1
else
echo -n -e \\a
fi
done
}

delay=$1
msg=$2

case $1 in
[0-9] | [0-9][0-9] | [0-9][0-9][0-9] )
alarm &
;;
*)
echo 'usage: minutes value must be in range 0 to 999'
;;
esac

2047
$ cat numbers.txt
1
2
3
4
5
6
7
8
9
10
$ sum=0; while read num ; do sum=$(($sum + $num)); done < numbers.txt ; echo $sum
55

2048
ในกรณี ที่ซื้อ cert จาก พวก เว็บ verisign , godaddy ไรงี้ ให้เราทำการ gen ค่าดังนี้

#openssl req -new -newkey rsa:2048 -nodes -sha256 -out web.csr
ใส่ค่า passparse
ใส่ข้อมูลต่างๆ ลงไปเช่น พวก ประเทศ ที่อยู่ office

แล้ว ส่งค่า web.csr เป็นไฟล์ cerificate request ไปให้ทางนั้น เค้าจะ gen server.crt มาให้เรา
แล้วเราก็นำมาเก็บไว้ใน path ที่ httpd-ssl.conf เรียกใช้ ครับ

2049
ปกติเรา สามารถ gen (.csr) request ssl cert ได้

ปกติแล้วผมจะใช้ openssl command ครับจัดยังไงนั้นไปดูกันเลย

โดยเริ่มสั่ง ตามนี้

ทำการสร้างคีย์ Key สำหรับ SSL ที่เป็น Key length ขนาด 2048 bit เพราะทาง NIST องค์กรจัดเรื่อง Key ssl
แนะนำให้ใช้เป็นขนาด 2048 bit ก่อน Jan 2014 เพื่อความปลอดภัยครับ ทำไมถึงเป็นเช่นนั้นจะอธิบายต่อไปในกระทู้ล่างๆ ครับผม
และก็ทำให้ support sha256

#openssl req -new -newkey rsa:2048 -nodes -sha256 -out web.csr
ใส่ข้อมูลต่างๆ ลงไปเช่น พวก ประเทศ ที่อยู่ office

ในกรณีถ้าไม่ได้ ไปซื้อ cert เพื่อความน่าเชื่อถือแล้ว แต่ว่าแค่ gen ใช้ในเครื่องก็สามารถ gen crt
ได้ดังนี้ ครับ
#openssl x509 -req -sha256 -days 365 -in /root/server.csr -signkey /root/server.key -out /root/server.crt
#/usr/bin/openssl req -x509 -sha256 -days 9000 -nodes -newkey rsa:4096 -keyout /etc/exim.key -out /etc/exim.cert

แล้ว copy ไปใน path ที่ไฟล์ extra/httpd-ssl.conf เรียกใช้งานดังนี้

SSLCertificateFile "/usr/local/etc/apache22/server.crt"
SSLCertificateKeyFile "/usr/local/etc/apache22/server.key"

เสร็จทำการ restart apache 1 ครั้ง
#/usr/local/etc/rc.d/apache22 restart

เสร็จแล้วลอง เข้าไปดู ข้อมูลใน certificate ได้ครับ

ถ้าจะสร้างเป็นแบบ SAN cert ที่มีหลายชื่อ ให้ใช้แบบนี้ครับ
#openssl req -new -newkey rsa:2048 -nodes -sha256 -out web.csr -config san.cnf

โดยที่ใน san.cnf มี configure ดังนี้
[ req ]
default_bits       = 2048
distinguished_name = req_distinguished_name
req_extensions = v3_req

[req_distinguished_name]
countryName = TH
countryName_default = TH
stateOrProvinceName = Bangkok
stateOrProvinceName_default = Bangkok
localityName = Pakkred
localityName_default = Pakkred
organizationalName = Packetlove
organizationalName_default = Packetlove
organizationalUnitName = IT
organizationalUnitName_default = IT
commonName                 = golfreeze.com
commonName_max = 64

[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1   = 2golfreeze2.com
DNS.2   = prim.com
DNS.3   = pkl.com
DNS.4   = domain4.com

2050
###innodb กับ myisam ต่างกันยังไง

ISAM (MyISAM) ซึ่งมีความรวดเร็วในการอ่านและเขียนสูง เนื่องจากมีการจัดเก็บไว้ในรูปแบบของแฟ้มข้อมูล
ซึ่งรองรับการอ่านข้อมูลพร้อมๆ กันได้ (เหมาะสำหรับ Web Application)
แต่อาจจะมีปัญหาเมื่อใช้งานกับระบบที่ต้องมีการอ่าน/เขียน ข้อมูลในตารางเดียวกัน พร้อมๆ กัน

ที่สำคัญ ฐานข้อมูลประเภท MyISAM จะมีปัญหาเรื่อง Index เสีย และ Data Corrupt บ่อยมาก หากใช้งานใน OS
ที่เป็น Windows และมีการ Shutdown อย่างไม่ถูกต้อง ซึ่งทำให้ผู้ดูแลระบบต้องมีการซ่อมแซม (repair table
bad_table) ตารางทีมีปัญหาอยู่เรื่อยๆ

InnoDB ข้อดีคือ รองรับการทำ Transaction รองรับการอ่านและเขียน พร้อมๆ กันได้ดีกว่าฐานข้อมูลประเภท
MyISAM และยังมีระบบ Auto Data Recovery หากมีการ shutdown โดยไม่เหมาะสม (ไฟดับ)

ซึ่งในการใช้งานผู้ใช้สามารถเลือกได้ว่าจะให้ตารางใดเป็นประเภท InnoDB หรือ MyISAM
ขึ้นอยู่กับความเหมาะสม (ว่าจะเลือกความเร็ว หรือ ประสิทธิภาพ)

สรุปความแตกต่างที่สำคัญระหว่าง InnoDB กับ Myisam ส่วนใหญ่ที่เด่นๆ เลยจะมี

1.InnoDB has row-level locking, MyISAM can only do full table-level locking.
2.InnoDB has better crash recovery.
3.MyISAM has FULLTEXT search indexes, InnoDB did not until MySQL 5.6 (Feb 2013).
4.InnoDB implements transactions, foreign keys and relationship constraints, MyISAM does not.

2051
สมมติว่าต้องการดู config file ที่ เป็นไฟล์ config ของ app สักตัวที่ ไม่เอาบันทัดที่มี เครื่องหมาย
\"#\" ขึ้นหน้า แล้วก็ลบบันทัดที่ว่างๆ ออก ต้องcommand นี้เลย

#more config.conf | grep -v ^# | sed '/^$/d' | more

2052
ในกรณีถ้า ลองเช๊ค

#cat /proc/drbd

แล้วเครื่อง master ขึ้นเป็นแบบนี้

xx00:/etc/postfix# cat /proc/drbd
version: 8.0.14 (api:86/proto:86)
GIT-hash: bb447522fc9a87d0069b7e14f0234911ebdab0f7 build by phil@fat-tyre, 2008-11-12 16:40:33
0: cs:WFConnection strimary/Unknown ds:UpToDate/DUnknown C r---
ns:78396 nr:24 dw:78472 dr:1556 al:38 bm:23 lo:0 pe:0 ua:0 ap:0
resync: used:0/61 hits:0 misses:0 starving:0 dirty:0 changed:0
act_log: used:0/127 hits:19574 misses:38 starving:0 dirty:0 changed:38

============================================
ดูตรง cs:WFConnection ใน pop00 ถ้าขึ้นอย่างนี้ ให้สั่ง sync ที่เครื่อง pop01 โดย
pop01#modprobe drbd
pop01#drbdadm connect all

ก็จะขึ้นเป็น cs:Connected แล้ว status จะเป็น Primary / Secondary ครับ

==============================================

ถ้าขึ้น cs:Standalone ที่เครื่อง pop00
ไม่ว่าจะ สั่ง command
pop00#drbdadm connect all
แล้วก็ยังไม่ connect กัน
ให้ทำการ check authkeys ว่าตรงกันหรือไม่
เสร็จแล้วสั่ง restart pop00 แล้วอีกแป๊บให้ restart pop01 ไปด้วยเลย
เมื่อ pop00 up ขึ้นมาจะแสดง status เป็น
cs:WFConnection strimary/Unknown
ก็ทำตามข้างล่าง นี่ได้เลย
============================================
แสดงว่า เครื่อง ที่ทำงานเป็น slave server (node = xx01)
ยังไม่ได้สั่งให้ connect กับ ตัว master ซึ่งจะขึ้นเป็น status
\\\"Unknown\\\"
ส่วนตัว (xx01
เป็น primary แล้ว) ให้จัดการ ที่เครื่อง xx01 โดย
xx01#modprobe drbd
xx01#drbdadm connect all

xx01#cat /proc/drbd

xx01:/home/golf# cat /proc/drbd
version: 8.3.0 (api:88/proto:86-89)
GIT-hash: 9ba8b93e24d842f0dd3fb1f9b90e8348ddb95829 build by root@pop01, 2009-10-26 15:47:27
0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r---
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0

แล้วสั่ง

xx01#/etc/init.d/heartbeat restart

ก็จะสามารถ ทำการ sync ได้ปกติครับ

2053
ขั้นตอนต่อไปมาทำ การ sync data กันก่อนระหว่าง xx00 กับ xx01

สั่งให้ xx00 ทำงานเป็น primary , xx01 ทำงานเป็น secondary ครับ

xx00#drbdadm -- --overwrite-data-of-peer primary r1

แนะนำให้ ใช้ ตัว screen ช่วยกันหลุดจาก session ครับ
xx00#screen

ทำการ ดูว่ามัน sync กันถึงไหนแล้ว
xx00#watch -n 1 'cat /proc/drbd'

2054
ช่วงนี้ได้ลองใช้ DRBD 8.3 ครับ กับตัว Debian 5.03 มา

ตั้งแต่ตอนลงก็ไม่ติดปัญหาอะไรจน มาเจอเกือบจะท้ายสุด create meta disk ไม่ได้ครับ ก็แก้ไขโดย


##Run fdisk to use /mbox and /meta on sdb HDD

#fdisk /dev/sdb

> d (delete all path)
>n (create new partition)
>p (primary)
>1
input (1-71500 cys) for sdb1 (mbox)
input (71501-end cys) for sdb2 (meta)

##Format to ext3 file system
#mkfs.ext3 /dev/sdb2

##create label
#e2label /dev/sdb2 /meta


####Then use this command to zero out path sdb2 (meta)
#dd if=/dev/zero of=/dev/sdb2 bs=1M count=128

###format device drbd0
#mkfs.ext3 /dev/drbd0

#modprobe drbd
#/etc/init.d/drbd restart


####Then create-md in drbdadm
#drbdadm create-md all
set priviledges to primary node only
#drbdadm -- --overwrite-data-of-peer primary all
#drbdadm attach r1
#drbdadm syncer r1
#drbdadm connect all
#mount /dev/drbd0 /mbox

#cat /proc/drbd

####Do same as server02
#drbdadm create-md all

####Then use this command to zero out path sdb2 (meta)
#dd if=/dev/zero of=/dev/sdb2 bs=1M count=128

###check status by
#cat /proc/drbd

2055
เพิ่มเติม config drbd.conf ของผมทั้งสองเครื่อง

โดยที่ xx00 , xx01 คือชื่อ host ของ server ครับ

==========================================
global { minor-count 1; }

resource r1 {
protocol C;
net {
shared-secret "xxxmail";
}
syncer {
rate 12M;
}

on xx00 {
device /dev/drbd0;
disk /dev/sdb1;
address 111.111.12.1:7788;
meta-disk /dev/sdb2[0];
}

on xx01 {
device /dev/drbd0;
disk /dev/sdb1;
address 111.111.12.3:7788;
meta-disk /dev/sdb2[0];
}
}
=============================================

ส่วนการลง app ดูได้จาก link ข้างล่างครับ

apt-get install make gcc libc6 flex linux-headers-`uname -r` libc6-dev linux-kernel-headers

cd /usr/src/
wgethttp://oss.linbit.com/drbd/8.3/drbd-8.3.0.tar.gz

tar -xzvf drbd-8.3.0.tar.gz
cd /usr/src/drbd-8.3.0/
make clean all
make install

modprobe drbd
cat /proc/drbd

ขั้นต่อไปทำการเตรียม drbd config (ทำเหมือนกันทั้งสองเครื่อง xx00,xx01)

xx00# drbdadm create-md r1
ถ้ามี error เกิดขึ้น
================================================
md_offset 597994237952
al_offset 597994205184
bm_offset 597975953408

Found ext3 filesystem which uses 583978752 kB
current configuration leaves usable 583960892 kB

Device size would be truncated, which
would corrupt data and result in
'access beyond end of device' errors.
You need to either
* use external meta data (recommended)
* shrink that filesystem first
* zero out the device (destroy the filesystem)
Operation refused.

Command 'drbdmeta /dev/drbd0 v08 /dev/sdb5 internal create-md' terminated with exit code 40
drbdadm create-md r1: exited with code 40
ให้ไปดู comment ที่ 8 ครับ
================================================

xx00# /etc/init.d/drbd start

xx01# drbdadm create-md r1
xx01# /etc/init.d/drbd start

Pages: 1 ... 135 136 [137] 138 139 ... 146