MySQL group by modulus

I have a series of scripts that acts on a subset of entries in a large (2M rows) MySQL table. There are 10 instances in the application, and each instance works on id‘s ending in 0-9 from a single table. The ending digit could also be considered the modulus. Instance 0 works on id‘s ending in 0, instance 1 works on id‘s ending in 1, etc.

mysql> select id from table limit 10;
+---------+
| id      |
+---------+
| 1099138 | 
| 1811048 | 
|  794297 | 
|  985812 | 
|  424120 | 
|  353809 | 
|  478882 | 
|  506101 | 
| 2184449 | 
| 1189511 | 
+---------+

To see what progress the application is making or if it is stuck at I use a query grouping the id‘s remaining to be worked on by modulus:

mysql> select substr(id, -1) as modulus,count(*) from table where field='criteria' group by modulus with rollup;
+---------+----------+
| modulus | count(*) |
+---------+----------+
| 0       |        9 | 
| 1       |       14 | 
| 2       |       13 | 
| 3       |     1985 | 
| 4       |       10 | 
| 5       |       16 | 
| 6       |       16 | 
| 7       |       11 | 
| 8       |       10 | 
| 9       |       20 | 
| NULL    |     2104 | 
+---------+----------+

The substr(id,-1) takes the id and returns the last digit (-1) as modulus. Then it’s grouped by modulus, and using rollup is summed at the end. As you can see the application still has 2104 id‘s to work on, and something is different about instance 3. Either there is an uneven distribution of id‘s ending in 3 (probably not the case, as id is primary key auto increment), or instance 3 in the application is borked.

get drive serial numbers from NetApp DS4243 shelves strapped to Linux server

When doing data recovery on a failed 3Ware RAID group, I utilized a couple spare NetApp DS4243 shelves. I put all the SATA drives into brackets, popped a quad port SAS card into a 1U server, and booted my rescue image from the network. The Debian OS was able to see each individual drive, at which point I could make ddrescue copies, and edit the 3Ware DCB metadata using hexedit. Keeping everything straight was quite a challenge. This one liner helped me find all the serials, and get the copies sorted out:

# for i in `ls /dev | grep sd | sed -e 's/[0-9]//' | sort -u`; do echo -n "/dev/$i " ; smartctl --all /dev/$i | grep "Serial number" | awk '{print " --- " $3}'; done;
/dev/sdaa  --- 9VS0R3T4
/dev/sdab  --- 9VS3SSJE
/dev/sdac  --- 9VS2K1DY
/dev/sdad  --- 9VS07FMW
/dev/sdae  --- 9VS402AA
/dev/sdaf  --- 9VS3V74N
/dev/sdag  --- 9VS388JX
/dev/sdah  --- 9VS2AQ9A
/dev/sdai  --- 9VS3THY9
/dev/sdaj  --- 9VS4EE3T
/dev/sdak  --- 9VS0FXWA
/dev/sdal  --- 9VS34DAD
/dev/sdam  --- 9VS45C9J
/dev/sdan  --- 9VS4L45S
/dev/sdao  --- 9VS2K9K1
/dev/sdap  --- 9VS2D631
/dev/sdaq  --- 9VS2L1BK
/dev/sdar  --- 9VS4DYDB
/dev/sdas  --- 9VS3T33R
/dev/sdat  --- 9VS3YE1K
/dev/sdc  --- 9VS1HWAM
/dev/sdd  --- 9VS1J9F3
/dev/sde  --- 9VS1L0FY
/dev/sdf  --- 9VS1H3RB
/dev/sdg  --- 9VS1JNPW
/dev/sdh  --- 9VS1GWGK
/dev/sdi  --- 9VS1DLZZ
/dev/sdj  --- 9VS1FSRD
/dev/sdk  --- 9VS3A8GZ
/dev/sdl  --- 9VS1L8ZZ
/dev/sdm  --- 9VS1JE7E
/dev/sdn  --- 9VS1CHE1
/dev/sdo  --- 9VS295R5
/dev/sdp  --- 9VS1HR8P
/dev/sdq  --- 9VS1EJQW
/dev/sdr  --- 9VS1A4V5
/dev/sds  --- 9VS1JGP8
/dev/sdt  --- 9VS1HPGB
/dev/sdu  --- 9VS1JAWZ
/dev/sdv  --- 9VS1JG8K
/dev/sdw  --- 9VS1JA51
/dev/sdx  --- WD-WMATV4441330
/dev/sdy  --- 9VS38GT3
/dev/sdz  --- 9VS4SQJX
#

append file with wget

To append a file with one fetched from a URL, use wget and output to STDOUT then redirect and append where needed.

# wget -q 'http://10.11.178.141/all_pub_keys' -O - >>/root/.ssh/authorized_keys

In this case I wanted to add some public keys to an existing authorized_keys file.

show progress for dd

By default dd is silent. It just copies whatever blocks you want from in to out. In order to see progress, send it a USR1 signal using kill.

Start a useless dd:

# dd if=/dev/zero of=/dev/null

In another terminal find the pid:

# ps aux | grep dd | grep -v grep
root      7784 90.5  0.0   2884   560 pts/9    R+   10:01   0:06 dd if /dev/zero of /dev/null
#
# kill -USR1 7784

The original window will now show this:

# dd if=/dev/zero of=/dev/null
14501614+0 records in
14501614+0 records out
7424826368 bytes (7.4 GB) copied, 16.2149 seconds, 458 MB/s

Then you can ctrl+c it to get the final output:

# dd if=/dev/zero of=/dev/null
14501614+0 records in
14501614+0 records out
7424826368 bytes (7.4 GB) copied, 16.2149 seconds, 458 MB/s
16888077+0 records in
16888076+0 records out
8646694912 bytes (8.6 GB) copied, 19.3507 seconds, 447 MB/s

This one liner will start your dd, then monitor it and output progress every 20 seconds. Once the dd is finished it will stop and give your shell back.

dd if=/dev/zero of=/dev/null & pid=$! && sleep 20s && while true; do i=`ps aux | awk '{print $2}' | grep ^$pid$`; if [ "${i:-a}" !=  "$pid" ]; then break; fi; kill -USR1 $pid; sleep 20s; done;

chkconfig –list for Debian using sysv-rc-conf

In Redhat based distros you can use chkconfig to see what daemons are supposed to run at what run levels. In Debian you can use the sysv-rc-conf package.

Install it:

# apt-get install --yes sysv-rc-conf

Run it:



# sysv-rc-conf

┌ SysV Runlevel Config   -: stop service  =/+: start service  h: help  q: quit ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                                                                                                                                                                                           │
│ service      1       2       3       4       5       0       6       S                                                                                                                                                                    │
│ ----------------------------------------------------------------------------                                                                                                                                                              │
│ acpid       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ bootlogd    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ bootlogs    [X]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ console-s$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ cron        [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ fancontrol  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ halt        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ hdparm      [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ ifupdown    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ ifupdown-$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ kbd         [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ keyboard-$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ killprocs   [X]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ lm-sensors  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ lvm2        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ mdadm       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ mdadm-raid  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ module-in$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ mountover$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ multipath$  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ multipath$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ mysql       [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ ndbd        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ networking  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ nfs-common  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ ntp         [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ open-iscsi  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ portmap     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ postfix     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ procps      [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ rc.local    [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ reboot      [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ rmnologin   [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ rsyslog     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ sendsigs    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ single      [X]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ snmpd       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ ssh         [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ stop-boot$  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ stop-boot$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ udev        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ udev-mtab   [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ umountfs    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ umountroot  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ urandom     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]  

This shows a list of Runlevel configuration for daemons. For most user land stuff, you will just want 2, 3, 4, and 5. You can edit the list, and changes will be made when you exit. This is a graphical alternative to update-rc.d

get memcache statistics

To obtain some stats about a memcached process, use nc to talk directly to it:


# echo "stats" | nc -w 1  11211
STAT pid 1750
STAT uptime 29481383
STAT time 1369781775
STAT version 1.4.5
STAT pointer_size 64
STAT rusage_user 6974.991909
STAT rusage_system 13000.624488
STAT curr_connections 10
STAT total_connections 132871296
STAT connection_structures 1674
STAT cmd_get 227296759
STAT cmd_set 113549712
STAT cmd_flush 0
STAT get_hits 221783239
STAT get_misses 5513520
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 36444
STAT incr_hits 19304751
STAT decr_misses 3
STAT decr_hits 19367598
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 26222582271
STAT bytes_written 25905663432
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 27010093
STAT curr_items 366610
STAT total_items 113554333
STAT evictions 0
STAT reclaimed 5291352
END

#

printing large integers with awk

When printing with awk, it uses scientific notation by default. Take this snippet from an example file. The first column is a count of how many times a file is present, the second column is the md5sum of that file and the third is the number of bytes that the file is.

# tail -3 md5sums
  14737 113136892f2137aa0116093a524ade0b        53
  19402 1c7b413c3fa39d0fed40556d2658ac73        44
  52818 b7f10e862d0e82f77a86b522159ce3c8        45
#

If I wanted to sum up the number of files counted in this file, and how much total space they are all taking up, I do this:

# awk '{i=i+$1;j=j+($3*$1);} END {print i; print j}' md5sums
22412000
1.45255e+13

So awk counted 22412000 files, totaling about 14.5 TB. Let’s make that a little more readable:

# awk '{i=i+$1;j=j+($3*$1);} END {printf ("%d\n", i); printf("%d\n", j)}' md5sums
22412000
2147483647

Um… that’s not right. But 2147483647 is a special number. You should recognize it as the maximum value of a 32 bit unsigned integer or ((2^32)/2)-1. In this case printf doesn’t handle large integers at all. Instead, use print, but tell awk what the output format should look like:

awk 'BEGIN {OFMT = "%.0f"} {i=i+$1;j=j+($3*$1);} END {print i; print j}' md5sums 
22412000
14525468874034

time mysql select without output

To see how long a query will take without actually outputting anything, change the pager in mysql client to /dev/null instead of STDOUT

mysql> pager;
Default pager wasn't set, using stdout.

mysql> pager >/dev/null
PAGER set to '>/dev/null'

mysql> select md5sum from table1;
5245618 rows in set (1 min 21.50 sec)

mysql> select md5sum from table1 where md5sum is not NULL and md5sum!='not calculated';
4832676 rows in set (1 min 11.52 sec)

mysql> select md5sum from table1 where md5sum not in ('NULL', '', 'not calculated');
4832078 rows in set (1 min 21.91 sec)

blacklist debian package

After upgrading synergy, every time I would hit the shift key on the remote screen, it would crash. I found the bug report, and decided to roll back the package. To make sure it didn’t get upgraded automatically, I did this:

# echo "synergy hold" | dpkg --set-selections

Of course this will also work for Ubuntu.