Skip to content

Instantly share code, notes, and snippets.

View icelander's full-sized avatar

Paul Rothrock icelander

View GitHub Profile
@icelander
icelander / members_to_csv.rb
Created August 13, 2020 21:13
members_to_csv.rb is a Ruby script that generates a CSV file containing the users who are members of a team or channel
#!/bin/ruby
require 'csv'
require 'yaml'
require 'httparty'
require 'uri'
# ## members_to_csv.rb
#
# ### About
@icelander
icelander / post_as_a_bot.rb
Last active August 13, 2020 21:46
post_as_a_bot.rb is a Ruby script that posts a message in a Mattermost channel as a bot user
#!/bin/ruby
require 'uri'
require 'httparty'
# ## post_as_a_bot.rb
#
# ### About
#
# post_as_a_bot.rb is a Ruby script that posts a message in a Mattermost channel as a bot user
@icelander
icelander / channel-and-team-audits.sql
Last active August 18, 2020 22:25
These queries help you find user memberships in the Mattermost database. Tested against schema version 5.24.0
-- Gets all channel memberships for Open and Private channels
SELECT t.DisplayName as TeamName,
c.DisplayName as ChannelName,
c.Type, -- O is a public channel, P is a private channel
u.FirstName,
u.LastName,
u.Username,
u.Email,
IF (cm.LastViewedAt!=0, FROM_UNIXTIME((cm.LastViewedAt/1000)), 'Never') AS LastViewed
FROM ChannelMembers cm
@icelander
icelander / mattermost_session_diagnostics.sql
Created August 24, 2020 17:28
This is a query that gets session diagnostic information from the Mattermost database
-- Find diagnostic information for a specific user's sessions
SELECT u.Id as UserId,
u.Username,
u.Email,
s.Id As SessionId,
s.Roles,
s.DeviceId, -- For mobile sessions
IF (s.ExpiresAt IS NOT NULL && s.ExpiresAt < (UNIX_TIMESTAMP() * 1000), "Expired", "Active") as SessionActive,
IF (FROM_UNIXTIME(s.ExpiresAt/1000) IS NULL, "Token", "User") as SessionType, -- Tokens are bot or personal access tokens, User are sessions where the user logged in
FROM_UNIXTIME(s.CreateAt/1000) AS SessionCreateDate,
@icelander
icelander / upgrade_mattermost.sh
Last active March 23, 2021 22:23
An attempt to automate upgrades
#!/bin/bash
## upgrade_mattermost.sh
#
## About
#
# This script was an attempt to automate upgrading Mattermost. Note the past
# tense.
#
## Usage

Navigating The Mattermost Database

NOTE: I highly recommend using a SQL client like pgAdmin or MySQL Workbench

Update: If you want to find out how many people have logged into your Mattermost server in the last 90 days this query is much more perfomant than the one using the Audits table:

MySQL

SELECT COUNT(*)
@icelander
icelander / team-to-okta.rb
Last active October 14, 2020 12:32
This script generates an Okta import file from a Mattermost team or channels
#!/usr/bin/env ruby
require 'csv'
require 'yaml'
require 'httparty'
require 'uri'
# ## members_to_okta.rb
#
# ### About
#!/usr/bin/env ruby
require 'smartsheet'
webhook_url = "https://mattermost.example.com/hooks/yu6twumt93n89cjibmhdgz6mpa"
smartsheet_api_key = "<SMARTSHEET API KEY>"
smartsheet_id = "<SMARTSHEET ID>" # File > Properties on sheet
# Initialize the client - use your access token here
smartsheet_client = Smartsheet::Client.new(token: smartsheet_api_key)
@icelander
icelander / make_ldap_users.rb
Last active November 4, 2020 15:02
Imports Mattermost users into an LDAP server from a bulk export file
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'net-ldap'
###
## make_ldap_users.rb
#
# This script automates the process of importing users from a Mattermost bulk
@icelander
icelander / get_info.sh
Created November 6, 2020 21:47
Gets diagnostic information for Mattermost application servers
#!/bin/bash
timestamp_string=`date -Iseconds`
parent_dir=`pwd`
dirname="$HOSTNAME-$timestamp_string"
dirpath="$parent_dir/$dirname"
tarname="$dirname.tgz"
tarpath="$parent_dir/$tarname"
if [[ $1 == "--help" || $1 == "-h" ]]; then