-
Notifications
You must be signed in to change notification settings - Fork 24
/
atom.xml
executable file
·1217 lines (938 loc) · 139 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Coding it my way</title>
<link href="https://madhur.co.in/blog/"/>
<updated>2024-05-01T17:58:18+05:30</updated>
<id>https://madhur.co.in/blog/</id>
<author>
<name>Madhur Ahuja</name>
<email>ahuja.madhur@gmail.com</email>
</author>
<entry>
<title>My Homelab Update - Part I</title>
<link href="https://madhur.co.in/blog/2024/05/01/my-homelab-update-part1.html"/>
<updated>2024-05-01T00:00:00+05:30</updated>
<id>id:/blog/2024/05/01/my-homelab-update-part1</id>
<content type="html"><p><a href="/blog/2024/01/07/proxmox-user-friendly-urls.html">Continuing my Homelab journey using Proxmox</a>, I have setup a full fledged home network with multiple services running.</p>
<p>Here is the quick diagram below for reference:</p>
<p><img src="/images/Blog/homenetwork.png" alt="" /></p>
<p>Some of the services I am running on my homenetwork as of now are</p>
<ul>
<li><a href="https://github.com/jellyfin/jellyfin">Jellyfin</a></li>
<li><a href="https://github.com/Athou/commafeed">Commmafeed</a></li>
<li><a href="https://github.com/prometheus/prometheus">Prometheus</a></li>
<li><a href="https://github.com/grafana/grafana">Grafana</a></li>
<li><a href="https://github.com/NginxProxyManager/nginx-proxy-manager">Nginx Proxy Manager</a></li>
<li><a href="https://snapdrop.net/">Snapdrop</a></li>
<li><a href="https://github.com/louislam/uptime-kuma">Uptime Kuma</a></li>
<li><a href="https://github.com/dgtlmoon/changedetection.io">Change Detection</a></li>
<li><a href="https://github.com/qbittorrent/qBittorrent">qBittorrent</a></li>
<li><a href="https://github.com/OliveTin/OliveTin">Olivetin</a></li>
<li><a href="https://github.com/dani-garcia/vaultwarden">Vaultwarden</a></li>
<li><a href="https://github.com/binwiederhier/ntfy">ntfy</a></li>
</ul>
<p>Some of the things I am trying to solve:</p>
<ul>
<li>Easily able to VPN from outside to access my password manager, RSS feeds, media etc</li>
<li>Get rid of <a href="https://www.pfsense.org/">PfSense</a>, PfSense is good but I feel its overkill for my needs.</li>
<li>Evaluate <a href="https://tailscale.com/">Tailscale</a>, <a href="https://www.wireguard.com/">Wireguard</a> and <a href="https://openvpn.net/">OpenVPN</a></li>
</ul>
<p>And finally,</p>
<ul>
<li>Having less power bill while powering my Proxmox server 24x7 :)</li>
</ul>
<p>Stay tuned for further updates if you are interested.</p>
</content>
</entry>
<entry>
<title>Quick Websockets client in Python</title>
<link href="https://madhur.co.in/blog/2024/04/13/quick-websockets-client-python.html"/>
<updated>2024-04-13T00:00:00+05:30</updated>
<id>id:/blog/2024/04/13/quick-websockets-client-python</id>
<content type="html"><p>When interacting with lot of websockets server, you need a nifty client to test out the behaviour of websocket server.</p>
<p>Till now, I have been using <a href="https://github.com/websockets/wscat">wscat</a> as the command line utility to interact with websockets server.</p>
<p>However, this command line utility has several disadvantages:</p>
<ul>
<li>It doesn’t allow you to send custom ping messages</li>
<li>It doesn’t support authentication</li>
</ul>
<p>Due to these limitations, I wrote a simple python script below which can send a custom ping payload.</p>
<p>This little script helps to quickly test out websocket servers.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">websocket</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="n">session_id</span><span class="o">=</span><span class="s">"12345"</span>
<span class="n">ready_message</span> <span class="o">=</span> <span class="p">{</span><span class="s">"type"</span><span class="p">:</span><span class="s">"setup"</span><span class="p">,</span><span class="s">"sid"</span><span class="p">:</span><span class="n">session_id</span><span class="p">,</span><span class="s">"time"</span><span class="p">:</span><span class="mi">1712393684026</span><span class="p">,</span> <span class="s">"src"</span><span class="p">:</span> <span class="s">"79677227"</span><span class="p">}</span>
<span class="n">ping_payload</span><span class="o">=</span><span class="p">{</span><span class="s">"type"</span><span class="p">:</span><span class="s">"heartbeatreq"</span><span class="p">,</span><span class="s">"time"</span><span class="p">:</span><span class="mi">1712393714042</span><span class="p">,</span><span class="s">"src"</span><span class="p">:</span><span class="s">"79677227"</span><span class="p">}</span>
<span class="k">def</span> <span class="nf">on_message</span><span class="p">(</span><span class="n">wsapp</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Received: "</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_ping</span><span class="p">(</span><span class="n">wsapp</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Got a ping! A pong reply has already been automatically sent."</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_pong</span><span class="p">(</span><span class="n">wsapp</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="n">wsapp</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_open</span><span class="p">(</span><span class="n">wsapp</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"sending ready"</span><span class="p">)</span>
<span class="n">ready_msg</span> <span class="o">=</span> <span class="n">json</span><span class="p">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">ready_message</span><span class="p">)</span> <span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span>
<span class="n">wsapp</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="n">ready_msg</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_error</span><span class="p">(</span><span class="n">wsapp</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_close</span><span class="p">(</span><span class="n">wsapp</span><span class="p">,</span> <span class="n">close_status_code</span><span class="p">,</span> <span class="n">close_reason</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"closed"</span><span class="p">,</span> <span class="n">close_reason</span><span class="p">,</span> <span class="n">close_status_code</span><span class="p">)</span>
<span class="n">wsapp</span> <span class="o">=</span> <span class="n">websocket</span><span class="p">.</span><span class="n">WebSocketApp</span><span class="p">(</span><span class="s">"ws://localhost:8080"</span><span class="p">,</span>
<span class="n">on_message</span><span class="o">=</span><span class="n">on_message</span><span class="p">,</span> <span class="n">on_ping</span><span class="o">=</span><span class="n">on_ping</span><span class="p">,</span> <span class="n">on_pong</span><span class="o">=</span><span class="n">on_pong</span><span class="p">,</span> <span class="n">on_open</span><span class="o">=</span><span class="n">on_open</span><span class="p">,</span> <span class="n">on_error</span><span class="o">=</span><span class="n">on_error</span><span class="p">,</span> <span class="n">on_close</span><span class="o">=</span><span class="n">on_close</span><span class="p">)</span>
<span class="n">wsapp</span><span class="p">.</span><span class="n">run_forever</span><span class="p">(</span><span class="n">ping_interval</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">ping_timeout</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">ping_payload</span><span class="o">=</span><span class="n">json</span><span class="p">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">ping_payload</span><span class="p">)</span><span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">)</span>
</code></pre></div></div>
</content>
</entry>
<entry>
<title>Building RediSearch module in Amazon Linux 2</title>
<link href="https://madhur.co.in/blog/2024/03/08/building-redisearch-module.html"/>
<updated>2024-03-08T00:00:00+05:30</updated>
<id>id:/blog/2024/03/08/building-redisearch-module</id>
<content type="html"><p>RediSearch is a <a href="https://redis.io/">Redis</a> module that provides querying, secondary indexing, and full-text search for Redis. To use RediSearch, you first declare indexes on your Redis data. You can then use the RediSearch query language to query that data.</p>
<p>There is a very good <a href="https://github.com/RediSearch/redisearch-getting-started">RediSearch getting started</a> tutorial on github.</p>
<p>There are several steps in bulding RediSearch module. The following steps worked on Amazon Linux 2 machine for us:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>yum <span class="nb">install </span>git perl perl-DateTime perl-JSON perl-Capture-Tiny
git clone https://github.com/RediSearch/RediSearch.git
wget https://github.com/linux-test-project/lcov/releases/download/v2.0/lcov-2.0-1.noarch.rpm
rpm <span class="nt">-ivh</span> /root/lcov-2.0-1.noarch.rpm
<span class="nb">cd </span>RediSearch
make setup
make build
</code></pre></div></div>
<p>At this point, the module should be built at the following location</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./RediSearch/bin/linux-x64-release/search/redisearch.so
</code></pre></div></div>
<p>Copy it to <code class="language-plaintext highlighter-rouge">/etc/redis</code></p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cp</span> /root/RediSearch/bin/linux-x64-release/search/redisearch.so /etc/redis/
</code></pre></div></div>
<p>The module can be activated in redis conf as follows:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>loadmodule /etc/redis/redisearch.so
</code></pre></div></div>
</content>
</entry>
<entry>
<title>Websocket - Close Status codes</title>
<link href="https://madhur.co.in/blog/2024/02/11/websocket-close-status-codes.html"/>
<updated>2024-02-11T00:00:00+05:30</updated>
<id>id:/blog/2024/02/11/websocket-close-status-codes</id>
<content type="html"><h3 id="a-table-for-reference">A table for reference</h3>
<table>
<thead>
<tr>
<th>Close code (uint16)</th>
<th>Codename</th>
<th>Internal</th>
<th>Customizable</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>0 - 999</td>
<td> </td>
<td>Yes</td>
<td>No</td>
<td>Unused</td>
</tr>
<tr>
<td>1000</td>
<td><code class="language-plaintext highlighter-rouge">CLOSE_NORMAL</code></td>
<td>No</td>
<td>No</td>
<td>Successful operation / regular socket shutdown</td>
</tr>
<tr>
<td>1001</td>
<td><code class="language-plaintext highlighter-rouge">CLOSE_GOING_AWAY</code></td>
<td>No</td>
<td>No</td>
<td>Client is leaving (browser tab closing)</td>
</tr>
<tr>
<td>1002</td>
<td><code class="language-plaintext highlighter-rouge">CLOSE_PROTOCOL_ERROR</code></td>
<td>Yes</td>
<td>No</td>
<td>Endpoint received a malformed frame</td>
</tr>
<tr>
<td>1003</td>
<td><code class="language-plaintext highlighter-rouge">CLOSE_UNSUPPORTED</code></td>
<td>Yes</td>
<td>No</td>
<td>Endpoint received an unsupported frame (e.g. binary-only endpoint received text frame)</td>
</tr>
<tr>
<td>1004</td>
<td> </td>
<td>Yes</td>
<td>No</td>
<td>Reserved</td>
</tr>
<tr>
<td>1005</td>
<td><code class="language-plaintext highlighter-rouge">CLOSED_NO_STATUS</code></td>
<td>Yes</td>
<td>No</td>
<td>Expected close status, received none</td>
</tr>
<tr>
<td>1006</td>
<td><code class="language-plaintext highlighter-rouge">CLOSE_ABNORMAL</code></td>
<td>Yes</td>
<td>No</td>
<td>No close code frame has been receieved</td>
</tr>
<tr>
<td>1007</td>
<td><em>Unsupported payload</em></td>
<td>Yes</td>
<td>No</td>
<td>Endpoint received inconsistent message (e.g. malformed UTF-8)</td>
</tr>
<tr>
<td>1008</td>
<td><em>Policy violation</em></td>
<td>No</td>
<td>No</td>
<td>Generic code used for situations other than 1003 and 1009</td>
</tr>
<tr>
<td>1009</td>
<td><code class="language-plaintext highlighter-rouge">CLOSE_TOO_LARGE</code></td>
<td>No</td>
<td>No</td>
<td>Endpoint won’t process large frame</td>
</tr>
<tr>
<td>1010</td>
<td><em>Mandatory extension</em></td>
<td>No</td>
<td>No</td>
<td>Client wanted an extension which server did not negotiate</td>
</tr>
<tr>
<td>1011</td>
<td><em>Server error</em></td>
<td>No</td>
<td>No</td>
<td>Internal server error while operating</td>
</tr>
<tr>
<td>1012</td>
<td><em>Service restart</em></td>
<td>No</td>
<td>No</td>
<td>Server/service is restarting</td>
</tr>
<tr>
<td>1013</td>
<td><em>Try again later</em></td>
<td>No</td>
<td>No</td>
<td>Temporary server condition forced blocking client’s request</td>
</tr>
<tr>
<td>1014</td>
<td><em>Bad gateway</em></td>
<td>No</td>
<td>No</td>
<td>Server acting as gateway received an invalid response</td>
</tr>
<tr>
<td>1015</td>
<td><em>TLS handshake fail</em></td>
<td>Yes</td>
<td>No</td>
<td>Transport Layer Security handshake failure</td>
</tr>
<tr>
<td>1016 - 1999</td>
<td> </td>
<td>Yes</td>
<td>No</td>
<td>Reserved for later</td>
</tr>
<tr>
<td>2000 - 2999</td>
<td> </td>
<td>Yes</td>
<td>Yes</td>
<td>Reserved for websocket extensions</td>
</tr>
<tr>
<td>3000 - 3999</td>
<td> </td>
<td>No</td>
<td>Yes</td>
<td>Registered first come first serve at IANA</td>
</tr>
<tr>
<td>4000 - 4999</td>
<td> </td>
<td>No</td>
<td>Yes</td>
<td>Available for applications</td>
</tr>
</tbody>
</table>
</content>
</entry>
<entry>
<title>Proxmox - Setting up user friendly urls</title>
<link href="https://madhur.co.in/blog/2024/01/07/proxmox-user-friendly-urls.html"/>
<updated>2024-01-07T00:00:00+05:30</updated>
<id>id:/blog/2024/01/07/proxmox-user-friendly-urls</id>
<content type="html"><p>I have been recently tinkering up with idea of setting up a <a href="https://en.wikipedia.org/wiki/Home_server">homelab</a> and <a href="https://en.wikipedia.org/wiki/Self-hosting_(web_services)">self hosting</a> few services
such as <a href="https://en.wikipedia.org/wiki/Name_server">DNS Server</a>, <a href="https://en.wikipedia.org/wiki/Media_server">Media server</a>, <a href="https://en.wikipedia.org/wiki/News_aggregator">RSS Aggregator</a> etc along with some standard monitoring tools.</p>
<p>I evaluated two products, <a href="https://www.proxmox.com/en/">Proxmox</a> and <a href="https://unraid.net/">Unraid</a></p>
<p>After evaluating both, I choose Proxmox primarily because it seemed to fit my needs more than Unraid. Unraid is primarily useful if you have lot of data which needs to be made available to multiple services.</p>
<p>Whereas, Proxmox seems more suitable to run independent services altogether, either in [Docker](https://en.wikipedia.org/wiki/Docker_(software), <a href="https://en.wikipedia.org/wiki/Virtual_machine">VMs</a> or <a href="https://en.wikipedia.org/wiki/LXC">Linux Containers</a></p>
<p>For me, the size of the data was not going to be very much, atleast initially and I found the idea of running an open source software (Proxmox) more appealing than one I had to purchase (Unraid).</p>
<p>I had a spare 12 CORE / 32 GB machine on which I quickly setup Proxmox and I must say, I really like the way I was quickly able to run the following services in a day:</p>
<ul>
<li><a href="https://jellyfin.org/">Jellyfin</a> - Media server</li>
<li><a href="https://github.com/Athou/commafeed">Commafeed</a> - Open source RSS aggregator written in Java</li>
<li><a href="https://pi-hole.net/">PiHole</a> - DNS Server with Ad Blocking</li>
<li><a href="https://www.qbittorrent.org/">qbittorrent</a> - Torrent client</li>
<li><a href="https://nginxproxymanager.com/">Nginx Proxy Manager</a> - A simple reverse proxy</li>
<li><a href="https://prometheus.io/">Prometheus</a> - Well known time series database</li>
<li><a href="https://grafana.com/">Grafana</a> - Well known visualization platform</li>
</ul>
<p>The cool thing about <a href="https://www.proxmox.com/en/">Proxmox</a> is that it uses a <a href="https://www.ibm.com/topics/hypervisors">Type 1 Hypervisor</a> called <a href="https://ubuntu.com/blog/kvm-hyphervisor">Kernel-based Virtual Machine (KVM)</a> to run all the above mentioned services in <a href="https://wiki.debian.org/LXC">Linux Containers running on Debian Linux OS</a></p>
<p>What that essentially means is that all these services get their own unique IP Addresses on my LAN network as opposed to all running on a single IP Addresses and exposing on different ports. Type 1 Hypervisor means that they are able to directly access machine hardware and do not have the any additional layer or overhead of resource hungry VM’s.</p>
<p>Each LXC takes around 200 - 300 MB of disk space and around 512 MB of RAM , making them all fit in a single commodity machine.</p>
<p>One caveat of this approach is that now I have tons of IP addresses and ports for me to memorize to access these services when I require. Though I can bookmark them, I do not find it appealing.</p>
<p>Hence, I decided to setup a reverse proxy in nginx for each of them in order for me to reach them through a very user friendly url. Here is the screenshot of the setup:</p>
<p><img src="/images/nginx.png" height="600px" /></p>
<p>The above setup requires some manual setup of setting up a <a href="https://en.wikipedia.org/wiki/Hosts_(file)"><code class="language-plaintext highlighter-rouge">/etc/hosts</code></a> file with appropriate entries as follows</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Hosts for nginx proxy manager
192.168.1.133 nginx
192.168.1.133 plexui
192.168.1.133 tui
192.168.1.133 jellyfinui
192.168.1.133 grafanaui
192.168.1.133 promui
192.168.1.133 zkui
192.168.1.133 piholeui
192.168.1.133 rssui
</code></pre></div></div>
<p>Here <code class="language-plaintext highlighter-rouge">192.168.1.133</code> is the IP Address of <a href="https://github.com/NginxProxyManager/nginx-proxy-manager">Nginx Proxy Manager</a></p>
<p>With the above setup I can reach my services in friendly urls such as:</p>
<p>http://plexui, http://tui, http://jellyfinui, http://grafanaui etc.</p>
<h3 id="using-pihole-for-dns-records-instead-of-static-host-file">Using Pihole for DNS Records instead of static host file</h3>
<p>One might argue that these hosts entries need to be done in each individiaul client that needs to access Proxmox containers and is not a scaleable approach.</p>
<p>That is correct, the reason I went with hosts file approach is that right now, I only have a single client - my desktop PC and I do not foresee running PiHole 24x7.</p>
<p>Incase, I choose PiHole as my permanent DNS resolver and AdBlocker, the scaleable way would be to add these entries in PiHole itself so that it becomes available to any client within my network.</p>
</content>
</entry>
<entry>
<title>Using Kitty hints to parse maven output</title>
<link href="https://madhur.co.in/blog/2023/12/28/using-kitty-hints-parse-maven-output.html"/>
<updated>2023-12-28T00:00:00+05:30</updated>
<id>id:/blog/2023/12/28/using-kitty-hints-parse-maven-output</id>
<content type="html"><p><a href="https://github.com/kovidgoyal/kitty">Kitty</a> is a very fast and feature rich terminal which has soon become my favourite.</p>
<p>One of the powerful features of Kitty is its customizability.</p>
<p>One such feature is <a href="https://sw.kovidgoyal.net/kitty/kittens/hints/">Kitty hints</a></p>
<p>Kitty hints allows you to select text on terminal and perform an action through a shortcut key. It is completely customizeable.</p>
<p>Common kitty hints action include opening files, folders and opening web urls outputted in terminal.</p>
<p>One example where, I was able to customize it was to parse maven output and makes the file links clickable to land directly on the line numbers.</p>
<p>For example, consider this build output:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.618 s
[INFO] Finished at: 2023-12-28T10:08:38+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project reactive-programming: Compilation failure: Compilation failure:
[ERROR] /run/media/madhur/centos/home/madhur/github/personal/reactive-programming/src/main/java/in/co/madhur/mono/FileService.java:[17,9] invalid method declaration; return type required
[ERROR] /run/media/madhur/centos/home/madhur/github/personal/reactive-programming/src/main/java/in/co/madhur/mono/FileService.java:[17,20] illegal start of type
[ERROR] /run/media/madhur/centos/home/madhur/github/personal/reactive-programming/src/main/java/in/co/madhur/mono/Lec08MonoRunnable.java:[15,54] ';' expected
[ERROR] /run/media/madhur/centos/home/madhur/github/personal/reactive-programming/src/main/java/in/co/madhur/mono/Lec06SupplierRefactoring.java:[25,37] ';' expected
[ERROR] -&gt; [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
</code></pre></div></div>
<p>Using kitty hints, we can make the file paths to <code class="language-plaintext highlighter-rouge">FileService.java</code> , <code class="language-plaintext highlighter-rouge">Lec08MonoRunnable.java</code> and <code class="language-plaintext highlighter-rouge">Lec06SupplierRefactoring.java</code> clickable and land it directly on appropriate line numbers.</p>
<p>This required writing custom regex to parse this line and emitting two named capturing groups <code class="language-plaintext highlighter-rouge">path</code> and <code class="language-plaintext highlighter-rouge">line</code>.</p>
<p>This can be done in <code class="language-plaintext highlighter-rouge">kitty.conf</code> as :</p>
<div class="language-conf highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">map</span> <span class="n">kitty_mod</span>+<span class="n">p</span> <span class="n">kitten</span> <span class="n">hints</span> --<span class="n">type</span>=<span class="n">regex</span> --<span class="n">regex</span>=<span class="s2">"(?&lt;path&gt;(?:\/[\w-_^ ]+)+\/?(?:[\w.])+[^.]):\[(?&lt;line&gt;\d+),\d+\].?"</span> --<span class="n">program</span> <span class="s2">"launch /home/madhur/scripts/editor.py"</span>
</code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">editor.py</code> is a simple program which is invoked with parameters <code class="language-plaintext highlighter-rouge">path=...../FileService.java</code> and <code class="language-plaintext highlighter-rouge">line=17</code></p>
<p>In my case, I launch <a href="https://github.com/neovim/neovim"><code class="language-plaintext highlighter-rouge">Neovim</code></a> or <a href="https://www.jetbrains.com/idea/"><code class="language-plaintext highlighter-rouge">IntelliJ</code></a> depending upon if <code class="language-plaintext highlighter-rouge">Intellij</code> is previously running.</p>
<p>This can be simply done through following snippet:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#!/usr/bin/env python3
</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="n">myObject</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="p">.</span><span class="n">argv</span><span class="p">:</span>
<span class="k">if</span> <span class="s">'='</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">line</span><span class="p">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">).</span><span class="n">split</span><span class="p">(</span><span class="s">"="</span><span class="p">)</span>
<span class="n">myObject</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">print</span><span class="p">(</span><span class="n">myObject</span><span class="p">)</span>
<span class="n">idea_running</span> <span class="o">=</span> <span class="bp">False</span>
<span class="n">pl</span> <span class="o">=</span> <span class="n">subprocess</span><span class="p">.</span><span class="n">Popen</span><span class="p">([</span><span class="s">'ps'</span><span class="p">,</span> <span class="s">'aux'</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="p">.</span><span class="n">PIPE</span><span class="p">).</span><span class="n">communicate</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">list_ps</span> <span class="o">=</span> <span class="n">pl</span><span class="p">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'utf-8'</span><span class="p">)</span>
<span class="k">if</span> <span class="s">'intellij'</span> <span class="ow">in</span> <span class="n">list_ps</span><span class="p">:</span>
<span class="n">idea_running</span> <span class="o">=</span> <span class="bp">True</span>
<span class="k">if</span> <span class="n">idea_running</span> <span class="ow">is</span> <span class="bp">True</span> <span class="ow">and</span> <span class="s">'java'</span> <span class="ow">in</span> <span class="n">myObject</span><span class="p">[</span><span class="s">'path'</span><span class="p">]:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Intellij found"</span><span class="p">)</span>
<span class="n">os</span><span class="p">.</span><span class="n">system</span><span class="p">(</span><span class="s">"flatpak run com.jetbrains.IntelliJ-IDEA-Community "</span> <span class="o">+</span> <span class="n">myObject</span><span class="p">[</span><span class="s">'path'</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Intellij not found"</span><span class="p">)</span>
<span class="n">os</span><span class="p">.</span><span class="n">system</span><span class="p">(</span><span class="s">"nvim "</span> <span class="o">+</span> <span class="s">" +"</span><span class="o">+</span><span class="n">myObject</span><span class="p">[</span><span class="s">'line'</span><span class="p">]</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="n">myObject</span><span class="p">[</span><span class="s">'path'</span><span class="p">])</span>
</code></pre></div></div>
</content>
</entry>
<entry>
<title>Android SSL Pinning Bypass</title>
<link href="https://madhur.co.in/blog/2023/12/05/android-ssl-pinning-bypass.html"/>
<updated>2023-12-05T00:00:00+05:30</updated>
<id>id:/blog/2023/12/05/android-ssl-pinning-bypass</id>
<content type="html"><p>I have always been a tinkerer and loved to explore internal of applications.</p>
<p>Android has always fascinated me from the beginning partly because its mostly open source and allows you to customize. Other aspect of Android which has been useful is <a href="https://en.wikipedia.org/wiki/Rooting_(Android)">Rooting</a></p>
<p>Rooting allows you to take complete control over the application and customize some of the aspects which are not possible in non-rooted Android.</p>
<p>One area where rooting helps is capturing SSL network traffic of the applications. This helps in debugging and learning more about how networks works. In this post, we will see how can we capture the network traffic for inspection in Android. This is mostly for debugging and learning purpose.</p>
<h3 id="apk-mitm"><a href="https://github.com/shroudedcode/apk-mitm">Apk-mitm</a></h3>
<hr />
<p>APK mitm tools modified the original Android application to disable the SSL Pinning. This tool relies on <a href="https://apktool.org/">apktool</a> heavily.</p>
<p>This tool always doesn’t work. Recently there has been lot of <a href="https://github.com/shroudedcode/apk-mitm/issues/141">issues</a> which have no solutions.</p>
<p>However, if it works, this is the most easiest and straightforward way as the modified app can be installed on non-rooted device and just inspected through proxy such as <a href="https://www.charlesproxy.com/">Charles</a></p>
<h3 id="android-unpinner"><a href="https://github.com/mitmproxy/android-unpinner">Android Unpinner</a></h3>
<hr />
<p>This tool is different from Apk Mitm in a way that it bypasses the SSL pinning code only at runtime. Thus, you no longer have to disassemble and repackage the application.</p>
<p>This tool relies heavily on <a href="https://github.com/frida/frida-tools">Frida tools</a> for its job.</p>
<p>Again, this tool is not gauranteed to work 100% of the time.</p>
<h3 id="rooting"><a href="https://en.wikipedia.org/wiki/Rooting_(Android)">Rooting</a></h3>
<hr />
<p>If you can root your device, it is very easy to capture network traffic even if its from SSL pinned app. However, rooting can destroy warranty.</p>
<p>These days, most of the Android apps, will refuse to start if they detect that they are running on a rooted software.</p>
<p>Hence, this option is only viable if you have a spare device.</p>
<h3 id="rooted-avd"><a href="https://gitlab.com/newbit/rootAVD">Rooted AVD</a></h3>
<hr />
<p>This is my most preferred method and mostly always works. Since the purpose of network inspection is mostly debugging and learning, we can do it on AVD instead of actual device.</p>
<p><a href="https://gitlab.com/newbit/rootAVD">rootAVD</a> allows you to seemlessly root the running AVD device, using the simple command</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./rootAVD.sh system-images/android-33/google_apis_playstore/x86_64/ramdisk.img
</code></pre></div></div>
<p>where <code class="language-plaintext highlighter-rouge">system-images/android-33/google_apis_playstore/x86_64/ramdisk.img</code> is the path of AVD devices relative to <code class="language-plaintext highlighter-rouge">$ANDROID_HOME</code></p>
<p>Next step, would be to install <a href="https://github.com/NVISOsecurity/MagiskTrustUserCerts">MagiskTrustUserCerts</a> module of <a href="https://github.com/topjohnwu/Magisk">Magisk</a></p>
<blockquote>
<p>This module makes all installed user certificates part of the system certificate store, so that they will
automatically be used when building the trust chain. This module makes it unnecessary to add the
network_security_config property to an application’s manifest.</p>
</blockquote>
<p>Once installed, your Magisk modules section should look like this</p>
<p><img src="/images/magisk.png" height="600px" /></p>
<p>Now, the only step remaining is enabling proxy to intercept traffic. I use <a href="https://www.charlesproxy.com/">Charles proxy</a> and this opens the port 8888 for listening to traffic.</p>
<p>Enabling proxy on AVD is as simple as executing:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>adb shell settings put global http_proxy 192.168.1.252:8888
</code></pre></div></div>
<p>where <code class="language-plaintext highlighter-rouge">192.168.1.252</code> is the local IP address of the host machine.</p>
<p>Disabling proxy can be done using:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>adb shell settings put global http_proxy :0
</code></pre></div></div>
<hr />
<p>If you guys have been using more preferred way to inspect SSL traffic. Let me know in the comments.</p>
</content>
</entry>
<entry>
<title>Transfer files from k8s or docker shell to other host</title>
<link href="https://madhur.co.in/blog/2023/11/14/transfer-files-from-shell-k8s.html"/>
<updated>2023-11-14T00:00:00+05:30</updated>
<id>id:/blog/2023/11/14/transfer-files-from-shell-k8s</id>
<content type="html"><p>If you are anytime stuck in a shell where you want to transfer files out of it, and do not have full access such as <code class="language-plaintext highlighter-rouge">kubectl</code> or <code class="language-plaintext highlighter-rouge">docker exec</code>. Here is the nifty hack:</p>
<h3 id="method-1-using-ngrok">Method 1 (Using ngrok):</h3>
<p>Run a ngrok tunnel on the target computer</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ngrok http 5000
</code></pre></div></div>
<p>Run a simple node program to capture request body</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">express</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">express</span><span class="dl">'</span><span class="p">)</span>
<span class="kd">const</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">express</span><span class="p">()</span>
<span class="kd">const</span> <span class="nx">port</span> <span class="o">=</span> <span class="mi">80</span>
<span class="kd">var</span> <span class="nx">bodyParser</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">body-parser</span><span class="dl">'</span><span class="p">)</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span><span class="o">=&gt;</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">);</span>
<span class="p">})</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="nx">port</span><span class="p">,</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">`Example app listening at http://localhost:</span><span class="p">${</span><span class="nx">port</span><span class="p">}</span><span class="s2">`</span><span class="p">)</span>
<span class="p">})</span>
</code></pre></div></div>
<p>The disadvantage of this method is that post body size will be limited by terms dictated by <a href="https://ngrok.com/">ngrok</a></p>
<p>Another disadvantage is that you cannot use this to transfer sensitive info.</p>
<h3 id="method-2-using-netcat">Method 2 (using Netcat)</h3>
<p>Run the <a href="https://en.wikipedia.org/wiki/Netcat">netcat</a> server on the target computer</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>nc <span class="nt">-l</span> <span class="nt">-p</span> 5000 <span class="o">&gt;</span> file.txt &lt; /dev/null
</code></pre></div></div>
<p>Stream the file to the port from the source shell</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cat </span>anyfile.txt | netcat server.ip.here 5000
</code></pre></div></div>
<p>Here the target computer needs to be on internet. It can done via ngrok tcp tunnel or a small EC2 instance.</p>
</content>
</entry>
<entry>
<title>Intellij cannot launch because 'process 2' already running</title>
<link href="https://madhur.co.in/blog/2023/10/30/cannot-start-intellij.html"/>
<updated>2023-10-30T00:00:00+05:30</updated>
<id>id:/blog/2023/10/30/cannot-start-intellij</id>
<content type="html"><p>I have occasionally got the error while starting <a href="https://www.jetbrains.com/idea/">IntelliJ</a></p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>IntelliJ cannot launch because 'process 2' already running
</code></pre></div></div>
<p>The process ID sometimes changed, restarting or even re-installing Intellij doesn’t solve the problem.</p>
<p>I run the <a href="https://flatpak.org/">Flatpak</a> version of <a href="https://flathub.org/apps/com.jetbrains.IntelliJ-IDEA-Community">IntelliJ</a></p>
<p>There are multiple threads for this online which suggest different solutions:</p>
<p><a href="https://intellij-support.jetbrains.com/hc/en-us/community/posts/13541697317906-Error-while-opening-intellij-Cannot-connect-to-already-running-IDE-instance-Exception-Process-2-837-is-still-running-">https://intellij-support.jetbrains.com/hc/en-us/community/posts/13541697317906-Error-while-opening-intellij-Cannot-connect-to-already-running-IDE-instance-Exception-Process-2-837-is-still-running-</a></p>
<p><a href="https://intellij-support.jetbrains.com/hc/en-us/community/posts/10865358461202-Intellij-cannot-launch-because-process-2-already-running">https://intellij-support.jetbrains.com/hc/en-us/community/posts/10865358461202-Intellij-cannot-launch-because-process-2-already-running</a></p>
<p><a href="https://askubuntu.com/questions/1462372/ubuntu-cannot-launch-intellij-anymore">https://askubuntu.com/questions/1462372/ubuntu-cannot-launch-intellij-anymore</a></p>
<p><a href="https://www.reddit.com/r/pycharm/comments/15esk6r/cannot_connect_to_already_running_ide_instance/">https://www.reddit.com/r/pycharm/comments/15esk6r/cannot_connect_to_already_running_ide_instance/</a></p>
<p>The root of the problem is the <code class="language-plaintext highlighter-rouge">.lock</code> file which IntelliJ creates while starting up. If there is a unclean shutdown, there are chances that this file will exist and the product will throw the mentioned error.</p>
<p>Solution is to simply delete this file. This file resides in the preferences folder, which can be anywhere depending on the Operating System.</p>
<p>For me it resided in</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">rm</span> ~/.var/app/com.jetbrains.IntelliJ-IDEA-Community/config/JetBrains/IdeaIC2023.2/.lock
</code></pre></div></div>
<p>simply deleting this file, solves the problem.</p>
<p>For Mac OS, this file would potentially reside in</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/Library/Application Support/JetBrains/
</code></pre></div></div>
</content>
</entry>
<entry>
<title>Recovering from Arch Boot failure</title>
<link href="https://madhur.co.in/blog/2023/09/30/recovering-from-arch-boot.html"/>
<updated>2023-09-30T00:00:00+05:30</updated>
<id>id:/blog/2023/09/30/recovering-from-arch-boot</id>
<content type="html"><p>Recently I faced boot failure after upgrading my <a href="https://archlinux.org/">Arch Linux</a> system.</p>
<p>The error was</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ERROR: device <span class="s1">'UUID=1dd743ed-e0a5-4b0c-80ee-186e89eea401'</span> not found. Skipping fsck.
mount: /new_root: can<span class="s1">'t find UUID=1dd743ed-e0a5-4b0c-80ee-186e89eea401.
ERROR: failed to mount '</span>1dd743ed-e0a5-4b0c-80ee-186e89eea401<span class="s1">' on real root
You are now being dropped into an emergency shell.
sh: can'</span>t access <span class="nb">tty</span><span class="p">;</span> job control turned off
<span class="o">[</span>rootfs ~]# _
</code></pre></div></div>
<p>This could happen while building errorneous <a href="https://en.wikipedia.org/wiki/Initial_ramdisk">initramfs</a> image</p>
<p>It was evident from <code class="language-plaintext highlighter-rouge">/var/log/pacman.log</code> logs</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[2023-10-04T20:36:26+0530] [ALPM] running '90-mkinitcpio-install.hook'...
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] ==&gt; Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] ==&gt; Using configuration file: '/etc/mkinitcpio.conf'
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] -&gt; -k /boot/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/initramfs-linux.img
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] ==&gt; Starting build: '6.5.2-arch1-1'
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [base]
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [udev]
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [autodetect]
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] sort: cannot read: '/sys/devices/pci0000:00/0000:00:08.1/0000:08:00.3/usb3/3-1/3-1.2/3-1.2:1.2/ep_04/uevent': No such file or directory
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] modprobe: ERROR: missing parameters. See -h.
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [modconf]
[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [block]
[2023-10-04T20:36:27+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [keyboard]
[2023-10-04T20:36:27+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [keymap]
[2023-10-04T20:36:27+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [consolefont]
[2023-10-04T20:36:27+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: consolefont: no font found in configuration
[2023-10-04T20:36:27+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [filesystems]
[2023-10-04T20:36:27+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [fsck]
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] ==&gt; Generating module dependencies
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] ==&gt; Creating zstd-compressed initcpio image: '/boot/initramfs-linux.img'
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] ==&gt; Image generation successful
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] ==&gt; Building image from preset: /etc/mkinitcpio.d/linux.preset: 'fallback'
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] ==&gt; Using configuration file: '/etc/mkinitcpio.conf'
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] -&gt; -k /boot/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-fallback.img -S autodetect
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] ==&gt; Starting build: '6.5.2-arch1-1'
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [base]
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [udev]
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [modconf]
[2023-10-04T20:36:28+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [block]
[2023-10-04T20:36:29+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'qed'
[2023-10-04T20:36:29+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'bfa'
[2023-10-04T20:36:29+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'qla2xxx'
[2023-10-04T20:36:29+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'aic94xx'
[2023-10-04T20:36:29+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'wd719x'
[2023-10-04T20:36:29+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'qla1280'
[2023-10-04T20:36:30+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: Possibly missing firmware for module: 'xhci_pci'
[2023-10-04T20:36:31+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [keyboard]
[2023-10-04T20:36:32+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [keymap]
[2023-10-04T20:36:32+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [consolefont]
[2023-10-04T20:36:32+0530] [ALPM-SCRIPTLET] ==&gt; WARNING: consolefont: no font found in configuration
[2023-10-04T20:36:32+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [filesystems]
[2023-10-04T20:36:33+0530] [ALPM-SCRIPTLET] -&gt; Running build hook: [fsck]
[2023-10-04T20:36:34+0530] [ALPM-SCRIPTLET] ==&gt; Generating module dependencies
[2023-10-04T20:36:34+0530] [ALPM-SCRIPTLET] ==&gt; Creating zstd-compressed initcpio image: '/boot/initramfs-linux-fallback.img'
[2023-10-04T20:36:35+0530] [ALPM-SCRIPTLET] ==&gt; Image generation successful
</code></pre></div></div>
<p>The line which caused the issue during image generation:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[2023-10-04T20:36:26+0530] [ALPM-SCRIPTLET] sort: cannot read: '/sys/devices/pci0000:00/0000:00:08.1/0000:08:00.3/usb3/3-1/3-1.2/3-1.2:1.2/ep_04/uevent': No such file or directory
</code></pre></div></div>
<p>The solution to recover was to boot to another OS. I run CentOS Steam 8 as a dual boot. And then mount the root file system of arch on path <code class="language-plaintext highlighter-rouge">/run/media/madhur/arch</code></p>
<p>Run the following commands</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>mount <span class="nt">-t</span> proc /proc proc/
mount <span class="nt">-t</span> sysfs /sys sys/
mount <span class="nt">--rbind</span> /dev dev/
mount <span class="nt">--rbind</span> /sys/firmware/efi/efivars sys/firmware/efi/efivars/
<span class="nb">chroot</span> /run/media/madhur/arch
mikinitcpio <span class="nt">-p</span> linux
</code></pre></div></div>
<p>The final command will generate the new initramfs image</p>
<p>More information about this issue can be found in this <a href="https://bbs.archlinux.org/viewtopic.php?id=289336">thread</a></p>
</content>
</entry>
<entry>
<title>Ansible roles for installing and configuring Vitess</title>
<link href="https://madhur.co.in/blog/2023/08/30/ansible-vitess.html"/>
<updated>2023-08-30T00:00:00+05:30</updated>
<id>id:/blog/2023/08/30/ansible-vitess</id>
<content type="html"><h3 id="what-is-vitess">What is vitess?</h3>
<p>Vitess is a scaleable databases built on top of MySQL. One key feature of vitess is that it supports sharding.
Checkout <a href="https://vitess.io/">Vitess website</a> for more information</p>
<p>As part of working with Vitess, I have provisioned a <a href="https://github.com/madhur/ansible-vitess">set of Ansible roles</a> which help in setting up Vitess in <a href="https://www.vagrantup.com/">vagrant</a> / staging / production infrastructure.</p>
<p>Check out the <a href="https://github.com/madhur/ansible-vitess">github repo</a> for more information.</p>
</content>
</entry>
<entry>
<title>Creating vagrant basebox</title>
<link href="https://madhur.co.in/blog/2023/07/30/creating-vagrant-base-box.html"/>
<updated>2023-07-30T00:00:00+05:30</updated>
<id>id:/blog/2023/07/30/creating-vagrant-base-box</id>
<content type="html"><p>For me, <a href="https://www.vagrantup.com/">Vagrant</a> is the ultimate tool to simulate production like envrionments in the development machine.</p>
<p>Some people prefer <a href="https://www.docker.com/">Docker</a> as an alternative to Vagrant, because of being lightweight and quick to provision.</p>
<p>However, I feel Docker and Vagrant have their own places and their responsibilities do not overlap. Let me explain why.</p>
<ul>
<li>
<p>Docker is good for running application services. Ultimately, now a days, since production code is deployed in containers itself, docker provides good testing ground for testing out <a href="https://docs.docker.com/engine/reference/builder/">Dockerfile</a> and <a href="https://docs.docker.com/engine/reference/commandline/images/">images</a></p>
</li>
<li>
<p>Though application services are deployed on containers, the stateful sets have not really reached that maturity stage where the persistent stores are deployed in containers.</p>
</li>
</ul>
<p>Organizations still prefer to run the <a href="https://cassandra.apache.org/_/index.html">Cassandra</a>, <a href="https://redis.io/">Redis</a>, <a href="https://www.mongodb.com/">MongoDB</a>, <a href="https://www.mysql.com/">MySQL</a> in the cloud VM’s if those are self hosted, instead of the containers.</p>
<p>This is where Vagrant becomes a crucial tool for sysadmins.</p>
<p>Any sysadmin, who manages such infrastructure would find Vagrant an incredibly useful tool to replicate the entire setup on development environment and test out any changes in its development environment before proceeding to higher environments.</p>
<p>For example, someone testing out the Cassandra DB cluster upgrade, can test it out in Vagrant environment before doing it in staging / production environment. This can help save cost.</p>
<p>There are <a href="https://app.vagrantup.com/boxes/search">Vagrant boxes</a> available for each and every popular Operating systems such as <a href="https://app.vagrantup.com/centos/boxes/7">CentOS 7</a>, <a href="https://app.vagrantup.com/generic/boxes/centos8">CentOS 8</a> and <a href="https://app.vagrantup.com/debian/boxes/jessie64">Debian</a></p>
<p><a href="https://github.com/search?q=vagrant+mysql&amp;type=repositories">Github</a> is full of open source scripts to provision any type of server with Vagrant.</p>
<p>Sometimes, it is helpful to create your own <a href="https://developer.hashicorp.com/vagrant/docs/boxes/base">Vagrant base box</a> in order to avoid downloading big dependencies from package repositories such as <a href="https://wiki.debian.org/apt-get#:~:text=apt%2Dget%20is%20a%20tool,part%20of%20the%20DebianPackageManagement%20system.">apt</a> or <a href="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/managing_software_with_the_dnf_tool/index">dnf</a></p>
<p>The dependencies can be packaged inside Vagrant basebox itself so that when the box is provisioned, there is no need to download these cut shorting the time to provision.</p>
<p>Recently, I had such a scenario and found some important tips when creating own vagrant boxes.</p>
<ul>
<li>The official guidelines for creating base box for <a href="https://www.virtualbox.org/">Virtualbox</a> <a href="https://developer.hashicorp.com/vagrant/docs/providers/virtualbox/boxes">https://developer.hashicorp.com/vagrant/docs/providers/virtualbox/boxes</a> recommends installing <a href="https://www.virtualbox.org/manual/ch04.html">Virtual Box guest additions</a> in the basebox. Infact, it has been listed as a must.</li>
</ul>
<blockquote>
<p>VirtualBox Guest Additions must be installed so that things such as shared folders can function. Installing guest additions also usually improves performance since the guest OS can make some optimizations by knowing it is running within VirtualBox.</p>
</blockquote>
<p>This is one of the most illogical and idiotic advice according to me. Because, VirtualBox Guest additions package is tied to specific version of VirtualBox. Once you install VirtualBox Guest additions package and later upgrade or downgrade VirtualBox version, it is of no use. This creates a tight coupling between the consumer (developer) and the Vagrant Base box. According to me, installing Virtual Box Guest Additions package is best avoided unless you really need fancy two way synchronizations between host and guest folders, which I believe no serious sysadmin should care about.</p>
<ul>
<li>
<p>When you provision a Vagrant box, the folder in which <code class="language-plaintext highlighter-rouge">Vagrantfile</code> resides is mounted inside the guest machine as <code class="language-plaintext highlighter-rouge">/vagrant</code>. Before executing <code class="language-plaintext highlighter-rouge">vagrant package --base basebox</code>, which triggers the creation of basebox, make sure to delete the <code class="language-plaintext highlighter-rouge">/vagrant</code> folder else it will be packaged as a regular folder inside the basebox which in most cases you don’t want.</p>
</li>
<li>
<p>It is very important to keep the size of basebox as small as possible to make sure it does not occupy huge disk space when boxes are provisioned from the base box. Vagrant does the compression of the basebox, however it does the compression based on the binary analysis of the disk. If there has been lot of files in and out of the box, even after deleting those files, the space won’t be reclaimed by vagrant compression. Hence its best to zero out the empty space before initiating creation of box using</p>
</li>
</ul>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># Zero free space to aid VM compression</span>
<span class="nb">printf</span> <span class="s2">"STEP: Zero free space to aid VM compression</span><span class="se">\n</span><span class="s2">"</span>
<span class="nb">dd </span><span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span>/EMPTY <span class="nv">bs</span><span class="o">=</span>1M
<span class="nb">rm</span> <span class="nt">-f</span> /EMPTY
</code></pre></div></div>
<p>The above step should take care of filling the empty space with zeroes which vagrant would be able to compress out signifcantly.</p>
<ul>
<li>If you care about the space optimzation very deeply, there are certain files such as <a href="https://www.kernel.org/doc/man-pages/">man pages</a> and <a href="https://en.wikipedia.org/wiki/Logging_(computing)">log files</a> which can be removed from Linux OS. Have a look at this <a href="https://gist.github.com/carlessanagustin/2fb92e88f2068300a2ed">gist</a> for such tweaks to optimize. I am recopying the gist below incase gist is later unavailable:</li>
</ul>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="c">#!/bin/sh</span>
<span class="c"># Credits to:</span>
<span class="c"># - http://vstone.eu/reducing-vagrant-box-size/</span>
<span class="c"># - https://github.com/mitchellh/vagrant/issues/343</span>
<span class="c"># - https://gist.github.com/adrienbrault/3775253</span>
<span class="c">## for vagrant related tasks, uncomment vagrant comments</span>
<span class="c"># vagrant: Unmount project</span>
<span class="c">#printf "STEP: vagrant: Unmount project\n"</span>
<span class="c">#umount /vagrant</span>
<span class="c"># Remove APT cache</span>
<span class="nb">printf</span> <span class="s2">"STEP: Remove APT cache</span><span class="se">\n</span><span class="s2">"</span>
apt-get clean <span class="nt">-y</span>
apt-get autoclean <span class="nt">-y</span>
<span class="c"># Zero free space to aid VM compression</span>
<span class="nb">printf</span> <span class="s2">"STEP: Zero free space to aid VM compression</span><span class="se">\n</span><span class="s2">"</span>
<span class="nb">dd </span><span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span>/EMPTY <span class="nv">bs</span><span class="o">=</span>1M
<span class="nb">rm</span> <span class="nt">-f</span> /EMPTY
<span class="c"># Remove APT files</span>
<span class="nb">printf</span> <span class="s2">"STEP: Zero free space to aid VM compression</span><span class="se">\n</span><span class="s2">"</span>
find /var/lib/apt <span class="nt">-type</span> f | xargs <span class="nb">rm</span> <span class="nt">-f</span>
<span class="c"># Remove documentation files</span>
<span class="nb">printf</span> <span class="s2">"STEP: Remove documentation files</span><span class="se">\n</span><span class="s2">"</span>
find /var/lib/doc <span class="nt">-type</span> f | xargs <span class="nb">rm</span> <span class="nt">-f</span>
<span class="c"># vagrant: Remove Virtualbox specific files</span>
<span class="c">#printf "STEP: vagrant: Remove Virtualbox specific files\n"</span>
<span class="c">#rm -rf /usr/src/vboxguest* /usr/src/virtualbox-ose-guest*</span>
<span class="c"># Remove Linux headers</span>
<span class="nb">printf</span> <span class="s2">"STEP: Remove Linux headers</span><span class="se">\n</span><span class="s2">"</span>
<span class="nb">rm</span> <span class="nt">-rf</span> /usr/src/linux-headers<span class="k">*</span>
<span class="c"># Remove Unused locales (edit for your needs, this keeps only en* and pt_BR)</span>
<span class="nb">printf</span> <span class="s2">"STEP: Remove Unused locales (edit for your needs, this keeps only en* and pt_BR)
find</span><span class="se">\n</span><span class="s2">"</span>
find /usr/share/locale/<span class="o">{</span>af,am,ar,as,ast,az,bal,be,bg,bn,bn_IN,br,bs,byn,ca,cr,cs,csb,cy,da,de,de_AT,dz,el,en_AU,en_CA,eo,es,et,et_EE,eu,fa,fi,fo,fr,fur,ga,gez,gl,gu,haw,he,hi,hr,hu,hy,id,is,it,ja,ka,kk,km,kn,ko,kok,ku,ky,lg,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,nb,ne,nl,nn,no,nso,oc,or,pa,pl,ps,qu,ro,ru,rw,si,sk,sl,so,sq,sr,sr<span class="k">*</span>latin,sv,sw,ta,te,th,ti,tig,tk,tl,tr,tt,ur,urd,ve,vi,wa,wal,wo,xh,zh,zh_HK,zh_CN,zh_TW,zu<span class="o">}</span> <span class="nt">-type</span> d <span class="nt">-delete</span>
<span class="c"># Remove bash history</span>
<span class="nb">printf</span> <span class="s2">"STEP: Remove bash history</span><span class="se">\n</span><span class="s2">"</span>
<span class="nb">unset </span>HISTFILE
<span class="nb">rm</span> <span class="nt">-f</span> /root/.bash_history
<span class="c"># vagrant: Remove bash history</span>
<span class="c">#printf "STEP: vagrant: Remove bash history\n"</span>
<span class="c">#rm -f /home/vagrant/.bash_history</span>
<span class="c"># Cleanup log files</span>
<span class="nb">printf</span> <span class="s2">"STEP: Cleanup log files</span><span class="se">\n</span><span class="s2">"</span>
find /var/log <span class="nt">-type</span> f | <span class="k">while </span><span class="nb">read </span>f<span class="p">;</span> <span class="k">do </span><span class="nb">echo</span> <span class="nt">-ne</span> <span class="s1">''</span> <span class="o">&gt;</span> <span class="nv">$f</span><span class="p">;</span> <span class="k">done</span><span class="p">;</span>
<span class="c"># Whiteout root</span>
<span class="nb">printf</span> <span class="s2">"STEP: Whiteout root</span><span class="se">\n</span><span class="s2">"</span>
<span class="nv">count</span><span class="o">=</span><span class="sb">`</span><span class="nb">df</span> <span class="nt">--sync</span> <span class="nt">-kP</span> / | <span class="nb">tail</span> <span class="nt">-n1</span> | <span class="nb">awk</span> <span class="nt">-F</span> <span class="s1">' '</span> <span class="s1">'{print $4}'</span><span class="sb">`</span><span class="p">;</span>
<span class="nv">count</span><span class="o">=</span><span class="k">$((</span>count <span class="o">-=</span> <span class="m">1</span><span class="k">))</span>
<span class="nb">dd </span><span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span>/tmp/whitespace <span class="nv">bs</span><span class="o">=</span>1024 <span class="nv">count</span><span class="o">=</span><span class="nv">$count</span><span class="p">;</span>
<span class="nb">rm</span> /tmp/whitespace<span class="p">;</span>
<span class="c"># Whiteout /boot</span>
<span class="nb">printf</span> <span class="s2">"STEP: Whiteout /boot</span><span class="se">\n</span><span class="s2">"</span>
<span class="nv">count</span><span class="o">=</span><span class="sb">`</span><span class="nb">df</span> <span class="nt">--sync</span> <span class="nt">-kP</span> /boot | <span class="nb">tail</span> <span class="nt">-n1</span> | <span class="nb">awk</span> <span class="nt">-F</span> <span class="s1">' '</span> <span class="s1">'{print $4}'</span><span class="sb">`</span><span class="p">;</span>
<span class="nv">count</span><span class="o">=</span><span class="k">$((</span>count <span class="o">-=</span> <span class="m">1</span><span class="k">))</span>
<span class="nb">dd </span><span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span>/boot/whitespace <span class="nv">bs</span><span class="o">=</span>1024 <span class="nv">count</span><span class="o">=</span><span class="nv">$count</span><span class="p">;</span>
<span class="nb">rm</span> /boot/whitespace<span class="p">;</span>
<span class="c"># Whiteout swap </span>
<span class="nb">printf</span> <span class="s2">"STEP: Whiteout swap</span><span class="se">\n</span><span class="s2">"</span>
<span class="nv">swappart</span><span class="o">=</span><span class="sb">`</span><span class="nb">cat</span> /proc/swaps | <span class="nb">tail</span> <span class="nt">-n1</span> | <span class="nb">awk</span> <span class="nt">-F</span> <span class="s1">' '</span> <span class="s1">'{print $1}'</span><span class="sb">`</span>
swapoff <span class="nv">$swappart</span><span class="p">;</span>
<span class="nb">dd </span><span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span><span class="nv">$swappart</span><span class="p">;</span>
mkswap <span class="nv">$swappart</span><span class="p">;</span>
swapon <span class="nv">$swappart</span><span class="p">;</span>
</code></pre></div></div>
</content>
</entry>
<entry>
<title>Process huge log files</title>
<link href="https://madhur.co.in/blog/2023/06/10/processing-huge-log-files.html"/>
<updated>2023-06-10T00:00:00+05:30</updated>
<id>id:/blog/2023/06/10/processing-huge-log-files</id>
<content type="html"><p>Recently, I was faced with a situation where I had to process huge (in order of several gigabytes) log files.</p>
<p>The log files consisted of either <a href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV format</a> or <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html">AWS ALB logs format</a></p>
<p>Here the process means, I had to process each row to either filter it or store the valuable information from it for later analysis.</p>
<p>First, I attempted with python to parse it with the simple logic</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">file_name</span> <span class="o">=</span> <span class="n">home</span> <span class="o">+</span> <span class="s">'/Downloads/huge_file.csv'</span>
<span class="n">filtered_rows</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'./filtered.csv'</span><span class="p">,</span> <span class="s">'w'</span><span class="p">,</span> <span class="n">newline</span><span class="o">=</span><span class="s">''</span><span class="p">)</span> <span class="k">as</span> <span class="n">csvfile</span><span class="p">:</span>
<span class="n">dict_writer</span> <span class="o">=</span> <span class="n">csv</span><span class="p">.</span><span class="n">DictWriter</span><span class="p">(</span><span class="n">csvfile</span><span class="p">,</span> <span class="p">[</span><span class="s">"client_ip"</span><span class="p">,</span> <span class="s">"target_processing_time"</span><span class="p">,</span> <span class="s">"target_status_code"</span><span class="p">,</span> <span class="s">"received_bytes"</span><span class="p">,</span> <span class="s">"request_url"</span><span class="p">,</span> <span class="s">"request_creation_time"</span><span class="p">,</span> <span class="s">"ssl_protocol"</span><span class="p">,</span> <span class="s">"trace_id"</span> <span class="p">])</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">'r'</span><span class="p">)</span> <span class="k">as</span> <span class="n">csv_file</span><span class="p">:</span>
<span class="n">csv_reader</span> <span class="o">=</span> <span class="n">csv</span><span class="p">.</span><span class="n">DictReader</span><span class="p">(</span><span class="n">csv_file</span><span class="p">)</span>
<span class="n">line_count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">csv_reader</span><span class="p">:</span>
<span class="c1"># Do some processing
</span> <span class="n">filtered_rows</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
<span class="n">dict_writer</span><span class="p">.</span><span class="n">writerow</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</code></pre></div></div>
<p>The above program reads the CSV line by line and optionally writes few columns of it to another file.</p>
<p>This was done on a 12 GB CSV file and it took 4-5 hours.</p>
<p>I was not satisfied with it as I had to do multiple iterations on multiple such files. There had to be a better way.</p>
<p>Turned to <a href="https://go.dev/">Go</a> due to its concurrency features</p>
<p>There is a <a href="https://pkg.go.dev/encoding/csv">encoding/csv</a> package in go which can help read the CSV files. However, it is single threaded and does not make use of multiple goroutines.</p>
<p>Came across another package <a href="https://github.com/actforgood/bigcsvreader">github.com/actforgood/bigcsvreader</a> which can help read the big CSV files with the use of multiple routines.</p>
<p>Quickly wrote a program which reads the bunch of huge CSV files in a folder and applies processing logic to it. Below is the sample snippet:</p>
<p>The below program reads the folder of log files and processes each one after another. However, since the reading is done using multiple goroutines, the below program can process 12 GB of ALB log file in matter of few seconds. The log files in my case was the list of ALB log files from AWS. Note that ALB log files are space character separated.</p>
<p>Also, if the files are huge, we need to increase the buffer space in library so that its heap memory does not runs out.</p>
<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">bigCSV</span><span class="o">.</span><span class="n">ColumnsDelimiter</span> <span class="o">=</span> <span class="sc">' '</span>
<span class="n">bigCSV</span><span class="o">.</span><span class="n">BufferSize</span> <span class="o">=</span> <span class="m">81920</span>
</code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">processRow()</code> function receives each row and based on the process / filtering criteria publishes the data of type <code class="language-plaintext highlighter-rouge">ALBLog</code> on the <code class="language-plaintext highlighter-rouge">resultChan</code> channel.</p>
<p>There is another inline goroutine which reads all the data from <code class="language-plaintext highlighter-rouge">resultChan</code> channel and writes it to <code class="language-plaintext highlighter-rouge">filtered.csv</code></p>
<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="p">(</span>
<span class="s">"context"</span>
<span class="s">"encoding/csv"</span>
<span class="s">"fmt"</span>
<span class="s">"log"</span>
<span class="s">"os"</span>
<span class="s">"strings"</span>
<span class="s">"sync"</span>
<span class="s">"sync/atomic"</span>
<span class="s">"github.com/actforgood/bigcsvreader"</span>
<span class="s">"net/url"</span>
<span class="p">)</span>
<span class="k">const</span> <span class="n">noOfColumns</span> <span class="o">=</span> <span class="m">29</span>
<span class="k">var</span> <span class="n">rowCount</span> <span class="o">=</span> <span class="m">0</span>
<span class="k">type</span> <span class="n">ALBLog</span> <span class="k">struct</span> <span class="p">{</span>
<span class="n">ClientIP</span> <span class="kt">string</span>
<span class="n">TargetProcessingTime</span> <span class="kt">string</span>
<span class="n">TargetStatusCode</span> <span class="kt">string</span>
<span class="n">ReceivedBytes</span> <span class="kt">string</span>
<span class="n">RequestUrl</span> <span class="kt">string</span>
<span class="n">RequestCreationTime</span> <span class="kt">string</span>
<span class="n">SSLProtocol</span> <span class="kt">string</span>
<span class="n">TraceID</span> <span class="kt">string</span>
<span class="n">RawRow</span> <span class="p">[]</span><span class="kt">string</span>
<span class="p">}</span>